Home | History | Annotate | Line # | Download | only in procfs
procfs_vfsops.c revision 1.116.2.1
      1  1.116.2.1  perseant /*	$NetBSD: procfs_vfsops.c,v 1.116.2.1 2025/08/02 05:57:45 perseant 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.116.2.1  perseant __KERNEL_RCSID(0, "$NetBSD: procfs_vfsops.c,v 1.116.2.1 2025/08/02 05:57:45 perseant Exp $");
     80       1.29  jonathan 
     81       1.39       mrg #if defined(_KERNEL_OPT)
     82       1.29  jonathan #include "opt_compat_netbsd.h"
     83  1.116.2.1  perseant #include "opt_sysv_ipc.h"
     84  1.116.2.1  perseant #include "opt_mqueue.h"
     85       1.29  jonathan #endif
     86        1.1        pk 
     87        1.8   mycroft #include <sys/param.h>
     88      1.107     htodd #include <sys/atomic.h>
     89        1.8   mycroft #include <sys/buf.h>
     90       1.54  christos #include <sys/dirent.h>
     91       1.92   hannken #include <sys/file.h>
     92       1.92   hannken #include <sys/filedesc.h>
     93      1.113   hannken #include <sys/fstrans.h>
     94       1.64      elad #include <sys/kauth.h>
     95      1.107     htodd #include <sys/kernel.h>
     96       1.79    rumble #include <sys/module.h>
     97      1.107     htodd #include <sys/mount.h>
     98      1.107     htodd #include <sys/proc.h>
     99      1.107     htodd #include <sys/signalvar.h>
    100      1.107     htodd #include <sys/sysctl.h>
    101      1.107     htodd #include <sys/syslog.h>
    102      1.107     htodd #include <sys/systm.h>
    103      1.107     htodd #include <sys/time.h>
    104      1.107     htodd #include <sys/vnode.h>
    105       1.35       mrg 
    106       1.77  dholland #include <miscfs/genfs/genfs.h>
    107       1.77  dholland 
    108        1.9       cgd #include <miscfs/procfs/procfs.h>
    109       1.35       mrg 
    110       1.35       mrg #include <uvm/uvm_extern.h>			/* for PAGE_SIZE */
    111        1.1        pk 
    112  1.116.2.1  perseant MODULE(MODULE_CLASS_VFS, procfs, "ptrace_common"
    113  1.116.2.1  perseant #if defined(MQUEUE)
    114  1.116.2.1  perseant 				 ",mqueue"
    115  1.116.2.1  perseant #endif
    116  1.116.2.1  perseant #if defined(SYSVSHM) || defined(SYSVSEM) || defined(SYSVMSG)
    117  1.116.2.1  perseant 				 ",sysv_ipc"
    118  1.116.2.1  perseant #endif
    119  1.116.2.1  perseant );
    120       1.79    rumble 
    121       1.74     pooka VFS_PROTOS(procfs);
    122       1.52    atatat 
    123      1.112   hannken #define PROCFS_HASHSIZE	256
    124      1.114   hannken #define PROCFS_EXEC_HOOK ((void *)1)
    125      1.114   hannken #define PROCFS_EXIT_HOOK ((void *)2)
    126      1.112   hannken 
    127       1.84      elad static kauth_listener_t procfs_listener;
    128      1.113   hannken static void *procfs_exechook;
    129      1.114   hannken static void *procfs_exithook;
    130      1.112   hannken LIST_HEAD(hashhead, pfsnode);
    131      1.112   hannken static u_long procfs_hashmask;
    132      1.112   hannken static struct hashhead *procfs_hashtab;
    133      1.112   hannken static kmutex_t procfs_hashlock;
    134      1.112   hannken 
    135      1.112   hannken static struct hashhead *
    136      1.112   hannken procfs_hashhead(pid_t pid)
    137      1.112   hannken {
    138      1.112   hannken 
    139      1.112   hannken 	return &procfs_hashtab[pid & procfs_hashmask];
    140      1.112   hannken }
    141      1.112   hannken 
    142      1.112   hannken void
    143      1.112   hannken procfs_hashrem(struct pfsnode *pfs)
    144      1.112   hannken {
    145      1.112   hannken 
    146      1.112   hannken 	mutex_enter(&procfs_hashlock);
    147      1.112   hannken 	LIST_REMOVE(pfs, pfs_hash);
    148      1.112   hannken 	mutex_exit(&procfs_hashlock);
    149      1.112   hannken }
    150       1.84      elad 
    151        1.1        pk /*
    152        1.1        pk  * VFS Operations.
    153        1.1        pk  *
    154        1.1        pk  * mount system call
    155        1.1        pk  */
    156        1.1        pk /* ARGSUSED */
    157       1.25  christos int
    158       1.66  christos procfs_mount(
    159       1.66  christos     struct mount *mp,
    160       1.66  christos     const char *path,
    161       1.66  christos     void *data,
    162       1.75     pooka     size_t *data_len)
    163        1.1        pk {
    164       1.75     pooka 	struct lwp *l = curlwp;
    165       1.32      fvdl 	struct procfsmount *pmnt;
    166       1.71       dsl 	struct procfs_args *args = data;
    167       1.36      fvdl 	int error;
    168        1.1        pk 
    169       1.91      maxv 	if (args == NULL)
    170       1.91      maxv 		return EINVAL;
    171       1.91      maxv 
    172        1.9       cgd 	if (UIO_MX & (UIO_MX-1)) {
    173        1.9       cgd 		log(LOG_ERR, "procfs: invalid directory entry size");
    174        1.9       cgd 		return (EINVAL);
    175       1.43  christos 	}
    176       1.43  christos 
    177       1.43  christos 	if (mp->mnt_flag & MNT_GETARGS) {
    178       1.71       dsl 		if (*data_len < sizeof *args)
    179       1.71       dsl 			return EINVAL;
    180       1.71       dsl 
    181       1.43  christos 		pmnt = VFSTOPROC(mp);
    182       1.43  christos 		if (pmnt == NULL)
    183       1.43  christos 			return EIO;
    184       1.71       dsl 		args->version = PROCFS_ARGSVERSION;
    185       1.71       dsl 		args->flags = pmnt->pmnt_flags;
    186       1.71       dsl 		*data_len = sizeof *args;
    187       1.71       dsl 		return 0;
    188        1.1        pk 	}
    189        1.1        pk 
    190        1.9       cgd 	if (mp->mnt_flag & MNT_UPDATE)
    191        1.9       cgd 		return (EOPNOTSUPP);
    192        1.9       cgd 
    193       1.71       dsl 	if (*data_len >= sizeof *args && args->version != PROCFS_ARGSVERSION)
    194       1.71       dsl 		return EINVAL;
    195       1.36      fvdl 
    196       1.87     rmind 	pmnt = kmem_zalloc(sizeof(struct procfsmount), KM_SLEEP);
    197       1.32      fvdl 
    198       1.86  christos 	mp->mnt_stat.f_namemax = PROCFS_MAXNAMLEN;
    199       1.58  jdolecek 	mp->mnt_flag |= MNT_LOCAL;
    200       1.42     soren 	mp->mnt_data = pmnt;
    201       1.34     assar 	vfs_getnewfsid(mp);
    202        1.9       cgd 
    203       1.53  christos 	error = set_statvfs_info(path, UIO_USERSPACE, "procfs", UIO_SYSSPACE,
    204       1.72     pooka 	    mp->mnt_op->vfs_name, mp, l);
    205       1.71       dsl 	if (*data_len >= sizeof *args)
    206       1.71       dsl 		pmnt->pmnt_flags = args->flags;
    207       1.71       dsl 	else
    208       1.71       dsl 		pmnt->pmnt_flags = 0;
    209       1.32      fvdl 
    210      1.104        ad 	mp->mnt_iflag |= IMNT_MPSAFE | IMNT_SHRLOOKUP;
    211       1.45  christos 	return error;
    212        1.1        pk }
    213        1.1        pk 
    214        1.1        pk /*
    215        1.1        pk  * unmount system call
    216        1.1        pk  */
    217       1.25  christos int
    218       1.75     pooka procfs_unmount(struct mount *mp, int mntflags)
    219        1.1        pk {
    220        1.9       cgd 	int error;
    221        1.9       cgd 	int flags = 0;
    222        1.9       cgd 
    223       1.28      fvdl 	if (mntflags & MNT_FORCE)
    224        1.9       cgd 		flags |= FORCECLOSE;
    225        1.9       cgd 
    226       1.25  christos 	if ((error = vflush(mp, 0, flags)) != 0)
    227        1.9       cgd 		return (error);
    228        1.9       cgd 
    229       1.87     rmind 	kmem_free(mp->mnt_data, sizeof(struct procfsmount));
    230       1.80    simonb 	mp->mnt_data = NULL;
    231       1.32      fvdl 
    232       1.87     rmind 	return 0;
    233        1.1        pk }
    234        1.1        pk 
    235       1.25  christos int
    236      1.102        ad procfs_root(struct mount *mp, int lktype, struct vnode **vpp)
    237        1.1        pk {
    238       1.88   hannken 	int error;
    239       1.88   hannken 
    240       1.92   hannken 	error = procfs_allocvp(mp, vpp, 0, PFSroot, -1);
    241       1.88   hannken 	if (error == 0) {
    242      1.102        ad 		error = vn_lock(*vpp, lktype);
    243       1.88   hannken 		if (error != 0) {
    244       1.88   hannken 			vrele(*vpp);
    245       1.88   hannken 			*vpp = NULL;
    246       1.88   hannken 		}
    247       1.88   hannken 	}
    248        1.1        pk 
    249       1.88   hannken 	return error;
    250        1.1        pk }
    251        1.1        pk 
    252        1.1        pk /* ARGSUSED */
    253       1.25  christos int
    254       1.75     pooka procfs_start(struct mount *mp, int flags)
    255        1.1        pk {
    256        1.9       cgd 
    257        1.9       cgd 	return (0);
    258        1.1        pk }
    259        1.1        pk 
    260        1.1        pk /*
    261        1.1        pk  * Get file system statistics.
    262        1.1        pk  */
    263       1.25  christos int
    264       1.75     pooka procfs_statvfs(struct mount *mp, struct statvfs *sbp)
    265        1.1        pk {
    266       1.16   mycroft 
    267       1.85     pooka 	genfs_statvfs(mp, sbp);
    268       1.85     pooka 
    269       1.16   mycroft 	sbp->f_bsize = PAGE_SIZE;
    270       1.53  christos 	sbp->f_frsize = PAGE_SIZE;
    271       1.14       cgd 	sbp->f_iosize = PAGE_SIZE;
    272       1.85     pooka 	sbp->f_blocks = 1;
    273      1.105   thorpej 	sbp->f_files = maxproc;					/* approx */
    274      1.105   thorpej 	sbp->f_ffree = maxproc - atomic_load_relaxed(&nprocs);	/* approx */
    275      1.105   thorpej 	sbp->f_favail = maxproc - atomic_load_relaxed(&nprocs);	/* approx */
    276       1.85     pooka 
    277        1.9       cgd 	return (0);
    278        1.1        pk }
    279        1.1        pk 
    280       1.25  christos /*ARGSUSED*/
    281       1.25  christos int
    282       1.66  christos procfs_sync(
    283       1.67  christos     struct mount *mp,
    284       1.67  christos     int waitfor,
    285       1.75     pooka     kauth_cred_t uc)
    286        1.1        pk {
    287        1.9       cgd 
    288        1.9       cgd 	return (0);
    289        1.1        pk }
    290        1.1        pk 
    291       1.25  christos /*ARGSUSED*/
    292       1.25  christos int
    293      1.102        ad procfs_vget(struct mount *mp, ino_t ino, int lktype,
    294       1.67  christos     struct vnode **vpp)
    295       1.16   mycroft {
    296       1.16   mycroft 	return (EOPNOTSUPP);
    297       1.16   mycroft }
    298       1.16   mycroft 
    299       1.92   hannken int
    300       1.92   hannken procfs_loadvnode(struct mount *mp, struct vnode *vp,
    301       1.92   hannken     const void *key, size_t key_len, const void **new_key)
    302       1.92   hannken {
    303       1.92   hannken 	int error;
    304       1.92   hannken 	struct pfskey pfskey;
    305       1.92   hannken 	struct pfsnode *pfs;
    306       1.92   hannken 
    307       1.92   hannken 	KASSERT(key_len == sizeof(pfskey));
    308       1.92   hannken 	memcpy(&pfskey, key, key_len);
    309       1.92   hannken 
    310       1.92   hannken 	pfs = kmem_alloc(sizeof(*pfs), KM_SLEEP);
    311       1.92   hannken 	pfs->pfs_pid = pfskey.pk_pid;
    312       1.92   hannken 	pfs->pfs_type = pfskey.pk_type;
    313       1.92   hannken 	pfs->pfs_fd = pfskey.pk_fd;
    314       1.92   hannken 	pfs->pfs_vnode = vp;
    315      1.112   hannken 	pfs->pfs_mount = mp;
    316       1.92   hannken 	pfs->pfs_flags = 0;
    317       1.92   hannken 	pfs->pfs_fileno =
    318       1.92   hannken 	    PROCFS_FILENO(pfs->pfs_pid, pfs->pfs_type, pfs->pfs_fd);
    319       1.92   hannken 	vp->v_tag = VT_PROCFS;
    320       1.92   hannken 	vp->v_op = procfs_vnodeop_p;
    321       1.92   hannken 	vp->v_data = pfs;
    322       1.92   hannken 
    323       1.92   hannken 	switch (pfs->pfs_type) {
    324       1.92   hannken 	case PFSroot:	/* /proc = dr-xr-xr-x */
    325       1.92   hannken 		vp->v_vflag |= VV_ROOT;
    326       1.92   hannken 		/*FALLTHROUGH*/
    327       1.92   hannken 	case PFSproc:	/* /proc/N = dr-xr-xr-x */
    328       1.92   hannken 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    329       1.92   hannken 		vp->v_type = VDIR;
    330       1.92   hannken 		break;
    331       1.92   hannken 
    332       1.92   hannken 	case PFStask:	/* /proc/N/task = dr-xr-xr-x */
    333       1.92   hannken 		if (pfs->pfs_fd == -1) {
    334       1.92   hannken 			pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
    335       1.92   hannken 			    S_IROTH|S_IXOTH;
    336       1.92   hannken 			vp->v_type = VDIR;
    337       1.92   hannken 			break;
    338       1.92   hannken 		}
    339       1.92   hannken 		/*FALLTHROUGH*/
    340       1.92   hannken 	case PFScurproc:	/* /proc/curproc = lr-xr-xr-x */
    341       1.92   hannken 	case PFSself:	/* /proc/self    = lr-xr-xr-x */
    342       1.92   hannken 	case PFScwd:	/* /proc/N/cwd = lr-xr-xr-x */
    343       1.92   hannken 	case PFSchroot:	/* /proc/N/chroot = lr-xr-xr-x */
    344       1.92   hannken 	case PFSexe:	/* /proc/N/exe = lr-xr-xr-x */
    345       1.92   hannken 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    346       1.92   hannken 		vp->v_type = VLNK;
    347       1.92   hannken 		break;
    348       1.92   hannken 
    349       1.92   hannken 	case PFSfd:
    350       1.92   hannken 		if (pfs->pfs_fd == -1) {	/* /proc/N/fd = dr-x------ */
    351       1.92   hannken 			pfs->pfs_mode = S_IRUSR|S_IXUSR;
    352       1.92   hannken 			vp->v_type = VDIR;
    353       1.92   hannken 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
    354       1.92   hannken 			file_t *fp;
    355       1.92   hannken 			vnode_t *vxp;
    356       1.92   hannken 			struct proc *p;
    357       1.92   hannken 
    358      1.109        ad 			mutex_enter(&proc_lock);
    359      1.108   thorpej 			p = procfs_proc_find(mp, pfs->pfs_pid);
    360      1.109        ad 			mutex_exit(&proc_lock);
    361       1.92   hannken 			if (p == NULL) {
    362       1.92   hannken 				error = ENOENT;
    363       1.92   hannken 				goto bad;
    364       1.92   hannken 			}
    365       1.92   hannken 			KASSERT(rw_read_held(&p->p_reflock));
    366       1.92   hannken 			if ((fp = fd_getfile2(p, pfs->pfs_fd)) == NULL) {
    367       1.92   hannken 				error = EBADF;
    368       1.92   hannken 				goto bad;
    369       1.92   hannken 			}
    370       1.92   hannken 
    371       1.92   hannken 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
    372       1.92   hannken 			switch (fp->f_type) {
    373       1.92   hannken 			case DTYPE_VNODE:
    374       1.93      matt 				vxp = fp->f_vnode;
    375       1.92   hannken 
    376       1.92   hannken 				/*
    377       1.92   hannken 				 * We make symlinks for directories
    378       1.92   hannken 				 * to avoid cycles.
    379       1.92   hannken 				 */
    380      1.111    bouyer 				if (vxp->v_type == VDIR ||
    381      1.111    bouyer 				    procfs_proc_is_linux_compat())
    382       1.92   hannken 					goto symlink;
    383       1.92   hannken 				vp->v_type = vxp->v_type;
    384       1.92   hannken 				break;
    385       1.92   hannken 			case DTYPE_PIPE:
    386       1.92   hannken 				vp->v_type = VFIFO;
    387       1.92   hannken 				break;
    388       1.92   hannken 			case DTYPE_SOCKET:
    389       1.92   hannken 				vp->v_type = VSOCK;
    390       1.92   hannken 				break;
    391       1.92   hannken 			case DTYPE_KQUEUE:
    392       1.92   hannken 			case DTYPE_MISC:
    393       1.92   hannken 			case DTYPE_SEM:
    394       1.92   hannken 			symlink:
    395       1.92   hannken 				pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|
    396       1.92   hannken 				    S_IXGRP|S_IROTH|S_IXOTH;
    397       1.92   hannken 				vp->v_type = VLNK;
    398       1.92   hannken 				break;
    399       1.92   hannken 			default:
    400       1.92   hannken 				error = EOPNOTSUPP;
    401       1.92   hannken 				closef(fp);
    402       1.92   hannken 				goto bad;
    403       1.92   hannken 			}
    404       1.92   hannken 			closef(fp);
    405       1.92   hannken 		}
    406       1.92   hannken 		break;
    407       1.92   hannken 
    408       1.92   hannken 	case PFSfile:	/* /proc/N/file = -rw------- */
    409       1.92   hannken 	case PFSmem:	/* /proc/N/mem = -rw------- */
    410       1.92   hannken 	case PFSregs:	/* /proc/N/regs = -rw------- */
    411       1.92   hannken 	case PFSfpregs:	/* /proc/N/fpregs = -rw------- */
    412       1.92   hannken 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    413       1.92   hannken 		vp->v_type = VREG;
    414       1.92   hannken 		break;
    415       1.92   hannken 
    416       1.92   hannken 	case PFSnote:	/* /proc/N/note = --w------ */
    417       1.92   hannken 	case PFSnotepg:	/* /proc/N/notepg = --w------ */
    418       1.92   hannken 		pfs->pfs_mode = S_IWUSR;
    419       1.92   hannken 		vp->v_type = VREG;
    420       1.92   hannken 		break;
    421       1.92   hannken 
    422       1.97  christos 	case PFSmap:		/* /proc/N/map = -r-------- */
    423       1.97  christos 	case PFSmaps:		/* /proc/N/maps = -r-------- */
    424       1.97  christos 	case PFSauxv:		/* /proc/N/auxv = -r-------- */
    425      1.110  riastrad 	case PFSenviron:	/* /proc/N/environ = -r-------- */
    426       1.97  christos 		pfs->pfs_mode = S_IRUSR;
    427       1.97  christos 		vp->v_type = VREG;
    428       1.97  christos 		break;
    429       1.97  christos 
    430       1.97  christos 	case PFSstatus:		/* /proc/N/status = -r--r--r-- */
    431       1.97  christos 	case PFSstat:		/* /proc/N/stat = -r--r--r-- */
    432       1.92   hannken 	case PFScmdline:	/* /proc/N/cmdline = -r--r--r-- */
    433       1.97  christos 	case PFSemul:		/* /proc/N/emul = -r--r--r-- */
    434       1.92   hannken 	case PFSmeminfo:	/* /proc/meminfo = -r--r--r-- */
    435       1.92   hannken 	case PFScpustat:	/* /proc/stat = -r--r--r-- */
    436       1.92   hannken 	case PFSdevices:	/* /proc/devices = -r--r--r-- */
    437       1.92   hannken 	case PFScpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
    438       1.97  christos 	case PFSuptime:		/* /proc/uptime = -r--r--r-- */
    439       1.97  christos 	case PFSmounts:		/* /proc/mounts = -r--r--r-- */
    440       1.92   hannken 	case PFSloadavg:	/* /proc/loadavg = -r--r--r-- */
    441       1.97  christos 	case PFSstatm:		/* /proc/N/statm = -r--r--r-- */
    442       1.92   hannken 	case PFSversion:	/* /proc/version = -r--r--r-- */
    443      1.115  christos 	case PFSlimit:		/* /proc/N/limit = -r--r--r-- */
    444      1.115  christos 	case PFSlimits:		/* /proc/N/limits = -r--r--r-- */
    445       1.92   hannken 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    446       1.92   hannken 		vp->v_type = VREG;
    447       1.92   hannken 		break;
    448       1.92   hannken 
    449  1.116.2.1  perseant 	case PFSsys:	/* /proc/sys = dr-xr-xr-x */
    450  1.116.2.1  perseant 	case PFSsysfs:	/* /proc/sys/fs = dr-xr-xr-x */
    451  1.116.2.1  perseant 	case PFSmqueue:	/* /proc/sys/fs/mqueue = dr-xr-xr-x */
    452      1.116  christos 	case PFSsysvipc:/* /proc/sysvipc = dr-xr-xr-x */
    453      1.116  christos 		if (pfs->pfs_fd == -1) {
    454      1.116  christos 			pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|
    455      1.116  christos 			    S_IROTH|S_IXOTH;
    456      1.116  christos 			vp->v_type = VDIR;
    457      1.116  christos 			break;
    458      1.116  christos 		}
    459      1.116  christos 		/*FALLTHROUGH*/
    460  1.116.2.1  perseant 	case PFSmq_msg_def:	/* /proc/sys/fs/mqueue/msg_default = -r--r--r-- */
    461  1.116.2.1  perseant 	case PFSmq_msg_max:	/* /proc/sys/fs/mqueue/msg_max = -r--r--r-- */
    462  1.116.2.1  perseant 	case PFSmq_siz_def:	/* /proc/sys/fs/mqueue/msgsize_default = -r--r--r-- */
    463  1.116.2.1  perseant 	case PFSmq_siz_max:	/* /proc/sys/fs/mqueue/msgsize_max = -r--r--r-- */
    464  1.116.2.1  perseant 	case PFSmq_qmax:	/* /proc/sys/fs/mqueue/queues_max = -r--r--r-- */
    465      1.116  christos 	case PFSsysvipc_msg:	/* /proc/sysvipc/msg = -r--r--r-- */
    466      1.116  christos 	case PFSsysvipc_sem:	/* /proc/sysvipc/sem = -r--r--r-- */
    467      1.116  christos 	case PFSsysvipc_shm:	/* /proc/sysvipc/shm = -r--r--r-- */
    468      1.116  christos 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    469      1.116  christos 		vp->v_type = VREG;
    470      1.116  christos 		break;
    471      1.116  christos 
    472       1.92   hannken #ifdef __HAVE_PROCFS_MACHDEP
    473       1.92   hannken 	PROCFS_MACHDEP_NODETYPE_CASES
    474       1.92   hannken 		procfs_machdep_allocvp(vp);
    475       1.92   hannken 		break;
    476       1.92   hannken #endif
    477       1.92   hannken 
    478       1.92   hannken 	default:
    479       1.92   hannken 		panic("procfs_allocvp");
    480       1.92   hannken 	}
    481       1.92   hannken 
    482      1.112   hannken 	mutex_enter(&procfs_hashlock);
    483      1.112   hannken 	LIST_INSERT_HEAD(procfs_hashhead(pfs->pfs_pid), pfs, pfs_hash);
    484      1.112   hannken 	mutex_exit(&procfs_hashlock);
    485      1.112   hannken 
    486       1.92   hannken 	uvm_vnp_setsize(vp, 0);
    487       1.92   hannken 	*new_key = &pfs->pfs_key;
    488       1.92   hannken 
    489       1.92   hannken 	return 0;
    490       1.92   hannken 
    491       1.92   hannken bad:
    492       1.92   hannken 	vp->v_tag =VT_NON;
    493       1.92   hannken 	vp->v_type = VNON;
    494       1.92   hannken 	vp->v_op = NULL;
    495       1.92   hannken 	vp->v_data = NULL;
    496       1.92   hannken 	kmem_free(pfs, sizeof(*pfs));
    497       1.92   hannken 	return error;
    498       1.92   hannken }
    499       1.92   hannken 
    500       1.25  christos void
    501       1.83    cegger procfs_init(void)
    502        1.1        pk {
    503       1.92   hannken 
    504        1.1        pk }
    505        1.9       cgd 
    506       1.33  jdolecek void
    507       1.83    cegger procfs_reinit(void)
    508       1.40       chs {
    509       1.92   hannken 
    510       1.40       chs }
    511       1.40       chs 
    512       1.40       chs void
    513       1.83    cegger procfs_done(void)
    514       1.33  jdolecek {
    515       1.92   hannken 
    516       1.33  jdolecek }
    517       1.33  jdolecek 
    518       1.37  jdolecek extern const struct vnodeopv_desc procfs_vnodeop_opv_desc;
    519       1.27   thorpej 
    520       1.37  jdolecek const struct vnodeopv_desc * const procfs_vnodeopv_descs[] = {
    521       1.27   thorpej 	&procfs_vnodeop_opv_desc,
    522       1.27   thorpej 	NULL,
    523       1.27   thorpej };
    524       1.27   thorpej 
    525        1.9       cgd struct vfsops procfs_vfsops = {
    526       1.90   hannken 	.vfs_name = MOUNT_PROCFS,
    527       1.90   hannken 	.vfs_min_mount_data = sizeof (struct procfs_args),
    528       1.90   hannken 	.vfs_mount = procfs_mount,
    529       1.90   hannken 	.vfs_start = procfs_start,
    530       1.90   hannken 	.vfs_unmount = procfs_unmount,
    531       1.90   hannken 	.vfs_root = procfs_root,
    532       1.90   hannken 	.vfs_quotactl = (void *)eopnotsupp,
    533       1.90   hannken 	.vfs_statvfs = procfs_statvfs,
    534       1.90   hannken 	.vfs_sync = procfs_sync,
    535       1.90   hannken 	.vfs_vget = procfs_vget,
    536       1.92   hannken 	.vfs_loadvnode = procfs_loadvnode,
    537       1.90   hannken 	.vfs_fhtovp = (void *)eopnotsupp,
    538       1.90   hannken 	.vfs_vptofh = (void *)eopnotsupp,
    539       1.90   hannken 	.vfs_init = procfs_init,
    540       1.90   hannken 	.vfs_reinit = procfs_reinit,
    541       1.90   hannken 	.vfs_done = procfs_done,
    542       1.90   hannken 	.vfs_snapshot = (void *)eopnotsupp,
    543       1.90   hannken 	.vfs_extattrctl = vfs_stdextattrctl,
    544       1.96   hannken 	.vfs_suspendctl = genfs_suspendctl,
    545       1.90   hannken 	.vfs_renamelock_enter = genfs_renamelock_enter,
    546       1.90   hannken 	.vfs_renamelock_exit = genfs_renamelock_exit,
    547       1.90   hannken 	.vfs_fsync = (void *)eopnotsupp,
    548       1.90   hannken 	.vfs_opv_descs = procfs_vnodeopv_descs
    549        1.9       cgd };
    550       1.79    rumble 
    551      1.113   hannken static void
    552      1.113   hannken procfs_exechook_cb(struct proc *p, void *arg)
    553      1.113   hannken {
    554      1.113   hannken 	struct hashhead *head;
    555      1.113   hannken 	struct pfsnode *pfs;
    556      1.113   hannken 	struct mount *mp;
    557      1.113   hannken 	struct pfskey key;
    558      1.113   hannken 	struct vnode *vp;
    559      1.113   hannken 	int error;
    560      1.113   hannken 
    561      1.114   hannken 	if (arg == PROCFS_EXEC_HOOK && !(p->p_flag & PK_SUGID))
    562      1.113   hannken 		return;
    563      1.113   hannken 
    564      1.113   hannken 	head = procfs_hashhead(p->p_pid);
    565      1.113   hannken 
    566      1.113   hannken again:
    567      1.113   hannken 	mutex_enter(&procfs_hashlock);
    568      1.113   hannken 	LIST_FOREACH(pfs, head, pfs_hash) {
    569      1.113   hannken 		if (pfs->pfs_pid != p->p_pid)
    570      1.113   hannken 			continue;
    571      1.113   hannken 		mp = pfs->pfs_mount;
    572      1.113   hannken 		key = pfs->pfs_key;
    573      1.113   hannken 		vfs_ref(mp);
    574      1.113   hannken 		mutex_exit(&procfs_hashlock);
    575      1.113   hannken 
    576      1.113   hannken 		error = vcache_get(mp, &key, sizeof(key), &vp);
    577      1.113   hannken 		vfs_rele(mp);
    578      1.113   hannken 		if (error != 0)
    579      1.113   hannken 			goto again;
    580      1.113   hannken 		if (vrecycle(vp))
    581      1.113   hannken 			goto again;
    582      1.113   hannken 		do {
    583      1.113   hannken 			error = vfs_suspend(mp, 0);
    584      1.113   hannken 		} while (error == EINTR || error == ERESTART);
    585      1.113   hannken 		vgone(vp);
    586      1.113   hannken 		if (error == 0)
    587      1.113   hannken 			vfs_resume(mp);
    588      1.113   hannken 		goto again;
    589      1.113   hannken 	}
    590      1.113   hannken 	mutex_exit(&procfs_hashlock);
    591      1.113   hannken }
    592      1.113   hannken 
    593       1.79    rumble static int
    594       1.84      elad procfs_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
    595       1.84      elad     void *arg0, void *arg1, void *arg2, void *arg3)
    596       1.84      elad {
    597       1.84      elad 	struct proc *p;
    598       1.84      elad 	struct pfsnode *pfs;
    599       1.84      elad 	int result;
    600       1.84      elad 
    601       1.84      elad 	result = KAUTH_RESULT_DEFER;
    602       1.84      elad 	p = arg0;
    603       1.84      elad 	pfs = arg1;
    604       1.84      elad 
    605       1.84      elad 	if (action != KAUTH_PROCESS_PROCFS)
    606       1.84      elad 		return result;
    607       1.84      elad 
    608       1.84      elad 	switch (pfs->pfs_type) {
    609       1.84      elad 	case PFSregs:
    610       1.84      elad 	case PFSfpregs:
    611       1.84      elad 	case PFSmem:
    612       1.84      elad 		if (kauth_cred_getuid(cred) != kauth_cred_getuid(p->p_cred) ||
    613       1.84      elad 		    ISSET(p->p_flag, PK_SUGID))
    614       1.84      elad 			break;
    615       1.84      elad 
    616       1.84      elad 		/*FALLTHROUGH*/
    617       1.84      elad 	default:
    618       1.84      elad 		result = KAUTH_RESULT_ALLOW;
    619       1.84      elad 		break;
    620       1.84      elad 	}
    621       1.84      elad 
    622       1.84      elad 	return result;
    623       1.84      elad }
    624       1.84      elad 
    625      1.103  pgoyette SYSCTL_SETUP(procfs_sysctl_setup, "procfs sysctl")
    626      1.103  pgoyette {
    627      1.103  pgoyette 
    628      1.103  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    629      1.103  pgoyette 		       CTLFLAG_PERMANENT,
    630      1.103  pgoyette 		       CTLTYPE_NODE, "procfs",
    631      1.103  pgoyette 		       SYSCTL_DESCR("Process file system"),
    632      1.103  pgoyette 		       NULL, 0, NULL, 0,
    633      1.103  pgoyette 		       CTL_VFS, 12, CTL_EOL);
    634      1.103  pgoyette 	/*
    635      1.103  pgoyette 	 * XXX the "12" above could be dynamic, thereby eliminating
    636      1.103  pgoyette 	 * one more instance of the "number to vfs" mapping problem,
    637      1.103  pgoyette 	 * but "12" is the order as taken from sys/mount.h
    638      1.103  pgoyette 	 */
    639      1.103  pgoyette }
    640       1.84      elad 
    641       1.84      elad static int
    642       1.79    rumble procfs_modcmd(modcmd_t cmd, void *arg)
    643       1.79    rumble {
    644       1.81    rumble 	int error;
    645       1.79    rumble 
    646       1.79    rumble 	switch (cmd) {
    647       1.79    rumble 	case MODULE_CMD_INIT:
    648       1.81    rumble 		error = vfs_attach(&procfs_vfsops);
    649       1.81    rumble 		if (error != 0)
    650       1.81    rumble 			break;
    651       1.84      elad 
    652       1.84      elad 		procfs_listener = kauth_listen_scope(KAUTH_SCOPE_PROCESS,
    653       1.84      elad 		    procfs_listener_cb, NULL);
    654       1.84      elad 
    655      1.114   hannken 		procfs_exechook = exechook_establish(procfs_exechook_cb,
    656      1.114   hannken 		    PROCFS_EXEC_HOOK);
    657      1.114   hannken 		procfs_exithook = exithook_establish(procfs_exechook_cb,
    658      1.114   hannken 		    PROCFS_EXIT_HOOK);
    659      1.113   hannken 
    660      1.113   hannken 		mutex_init(&procfs_hashlock, MUTEX_DEFAULT, IPL_NONE);
    661      1.113   hannken 		procfs_hashtab = hashinit(PROCFS_HASHSIZE, HASH_LIST, true,
    662      1.113   hannken 		    &procfs_hashmask);
    663      1.113   hannken 
    664       1.81    rumble 		break;
    665       1.79    rumble 	case MODULE_CMD_FINI:
    666       1.81    rumble 		error = vfs_detach(&procfs_vfsops);
    667       1.81    rumble 		if (error != 0)
    668       1.81    rumble 			break;
    669       1.84      elad 		kauth_unlisten_scope(procfs_listener);
    670      1.113   hannken 		exechook_disestablish(procfs_exechook);
    671      1.114   hannken 		exithook_disestablish(procfs_exithook);
    672      1.113   hannken 		mutex_destroy(&procfs_hashlock);
    673      1.113   hannken 		hashdone(procfs_hashtab, HASH_LIST, procfs_hashmask);
    674       1.81    rumble 		break;
    675       1.79    rumble 	default:
    676       1.81    rumble 		error = ENOTTY;
    677       1.81    rumble 		break;
    678       1.79    rumble 	}
    679       1.81    rumble 
    680       1.81    rumble 	return (error);
    681       1.79    rumble }
    682