Home | History | Annotate | Line # | Download | only in procfs
procfs_vnops.c revision 1.151
      1  1.151     pooka /*	$NetBSD: procfs_vnops.c,v 1.151 2007/02/19 00:08:18 pooka Exp $	*/
      2  1.145        ad 
      3  1.145        ad /*-
      4  1.147        ad  * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
      5  1.145        ad  * All rights reserved.
      6  1.145        ad  *
      7  1.145        ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.145        ad  * by Andrew Doran.
      9  1.145        ad  *
     10  1.145        ad  * Redistribution and use in source and binary forms, with or without
     11  1.145        ad  * modification, are permitted provided that the following conditions
     12  1.145        ad  * are met:
     13  1.145        ad  * 1. Redistributions of source code must retain the above copyright
     14  1.145        ad  *    notice, this list of conditions and the following disclaimer.
     15  1.145        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.145        ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.145        ad  *    documentation and/or other materials provided with the distribution.
     18  1.145        ad  * 3. All advertising materials mentioning features or use of this software
     19  1.145        ad  *    must display the following acknowledgement:
     20  1.145        ad  *	This product includes software developed by the NetBSD
     21  1.145        ad  *	Foundation, Inc. and its contributors.
     22  1.145        ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.145        ad  *    contributors may be used to endorse or promote products derived
     24  1.145        ad  *    from this software without specific prior written permission.
     25  1.145        ad  *
     26  1.145        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.145        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.145        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.145        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.145        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.145        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.145        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.145        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.145        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.145        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.145        ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.145        ad  */
     38   1.24       cgd 
     39    1.1        pk /*
     40   1.53      fvdl  * Copyright (c) 1993, 1995
     41   1.21   mycroft  *	The Regents of the University of California.  All rights reserved.
     42    1.2        pk  *
     43    1.9       cgd  * This code is derived from software contributed to Berkeley by
     44    1.9       cgd  * Jan-Simon Pendry.
     45    1.9       cgd  *
     46    1.2        pk  * Redistribution and use in source and binary forms, with or without
     47    1.2        pk  * modification, are permitted provided that the following conditions
     48    1.2        pk  * are met:
     49    1.2        pk  * 1. Redistributions of source code must retain the above copyright
     50    1.2        pk  *    notice, this list of conditions and the following disclaimer.
     51    1.2        pk  * 2. Redistributions in binary form must reproduce the above copyright
     52    1.2        pk  *    notice, this list of conditions and the following disclaimer in the
     53    1.2        pk  *    documentation and/or other materials provided with the distribution.
     54  1.107       agc  * 3. Neither the name of the University nor the names of its contributors
     55  1.107       agc  *    may be used to endorse or promote products derived from this software
     56  1.107       agc  *    without specific prior written permission.
     57  1.107       agc  *
     58  1.107       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.107       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.107       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.107       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.107       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.107       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.107       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.107       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.107       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.107       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.107       agc  * SUCH DAMAGE.
     69  1.107       agc  *
     70  1.107       agc  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
     71  1.107       agc  */
     72  1.107       agc 
     73  1.107       agc /*
     74  1.107       agc  * Copyright (c) 1993 Jan-Simon Pendry
     75  1.107       agc  *
     76  1.107       agc  * This code is derived from software contributed to Berkeley by
     77  1.107       agc  * Jan-Simon Pendry.
     78  1.107       agc  *
     79  1.107       agc  * Redistribution and use in source and binary forms, with or without
     80  1.107       agc  * modification, are permitted provided that the following conditions
     81  1.107       agc  * are met:
     82  1.107       agc  * 1. Redistributions of source code must retain the above copyright
     83  1.107       agc  *    notice, this list of conditions and the following disclaimer.
     84  1.107       agc  * 2. Redistributions in binary form must reproduce the above copyright
     85  1.107       agc  *    notice, this list of conditions and the following disclaimer in the
     86  1.107       agc  *    documentation and/or other materials provided with the distribution.
     87    1.2        pk  * 3. All advertising materials mentioning features or use of this software
     88    1.2        pk  *    must display the following acknowledgement:
     89    1.9       cgd  *	This product includes software developed by the University of
     90    1.9       cgd  *	California, Berkeley and its contributors.
     91    1.9       cgd  * 4. Neither the name of the University nor the names of its contributors
     92    1.9       cgd  *    may be used to endorse or promote products derived from this software
     93    1.9       cgd  *    without specific prior written permission.
     94    1.9       cgd  *
     95    1.9       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     96    1.9       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     97    1.9       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     98    1.9       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     99    1.9       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    100    1.9       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    101    1.9       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    102    1.9       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    103    1.9       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    104    1.9       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    105    1.9       cgd  * SUCH DAMAGE.
    106    1.2        pk  *
    107   1.53      fvdl  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
    108    1.2        pk  */
    109    1.2        pk 
    110    1.2        pk /*
    111    1.9       cgd  * procfs vnode interface
    112    1.1        pk  */
    113   1.85     lukem 
    114   1.85     lukem #include <sys/cdefs.h>
    115  1.151     pooka __KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.151 2007/02/19 00:08:18 pooka Exp $");
    116    1.1        pk 
    117    1.8   mycroft #include <sys/param.h>
    118    1.8   mycroft #include <sys/systm.h>
    119    1.8   mycroft #include <sys/time.h>
    120    1.8   mycroft #include <sys/kernel.h>
    121    1.8   mycroft #include <sys/file.h>
    122   1.91  christos #include <sys/filedesc.h>
    123    1.8   mycroft #include <sys/proc.h>
    124    1.8   mycroft #include <sys/vnode.h>
    125    1.8   mycroft #include <sys/namei.h>
    126    1.9       cgd #include <sys/malloc.h>
    127   1.76      fvdl #include <sys/mount.h>
    128   1.21   mycroft #include <sys/dirent.h>
    129    1.8   mycroft #include <sys/resourcevar.h>
    130   1.48   mycroft #include <sys/stat.h>
    131   1.89   thorpej #include <sys/ptrace.h>
    132  1.130      elad #include <sys/kauth.h>
    133   1.48   mycroft 
    134   1.71       mrg #include <uvm/uvm_extern.h>	/* for PAGE_SIZE */
    135   1.48   mycroft 
    136   1.21   mycroft #include <machine/reg.h>
    137   1.41   mycroft 
    138   1.41   mycroft #include <miscfs/genfs/genfs.h>
    139    1.9       cgd #include <miscfs/procfs/procfs.h>
    140   1.11       cgd 
    141    1.9       cgd /*
    142    1.9       cgd  * Vnode Operations.
    143    1.9       cgd  *
    144    1.9       cgd  */
    145    1.1        pk 
    146  1.128  christos static int procfs_validfile_linux(struct lwp *, struct mount *);
    147  1.117      yamt static int procfs_root_readdir_callback(struct proc *, void *);
    148  1.128  christos static struct vnode *procfs_dir(pfstype, struct lwp *, struct proc *,
    149  1.126    atatat 				char **, char *, int);
    150   1.74        tv 
    151    1.1        pk /*
    152    1.9       cgd  * This is a list of the valid names in the
    153    1.9       cgd  * process-specific sub-directories.  It is
    154    1.9       cgd  * used in procfs_lookup and procfs_readdir
    155    1.9       cgd  */
    156   1.97  jdolecek static const struct proc_target {
    157   1.32   mycroft 	u_char	pt_type;
    158   1.32   mycroft 	u_char	pt_namlen;
    159   1.97  jdolecek 	const char	*pt_name;
    160   1.32   mycroft 	pfstype	pt_pfstype;
    161  1.128  christos 	int	(*pt_valid)(struct lwp *, struct mount *);
    162   1.32   mycroft } proc_targets[] = {
    163    1.9       cgd #define N(s) sizeof(s)-1, s
    164   1.32   mycroft 	/*	  name		type		validp */
    165  1.136  christos 	{ DT_DIR, N("."),	PFSproc,	NULL },
    166  1.136  christos 	{ DT_DIR, N(".."),	PFSroot,	NULL },
    167  1.109     darcy 	{ DT_DIR, N("fd"),	PFSfd,		NULL },
    168  1.136  christos 	{ DT_REG, N("file"),	PFSfile,	procfs_validfile },
    169  1.109     darcy 	{ DT_REG, N("mem"),	PFSmem,		NULL },
    170  1.136  christos 	{ DT_REG, N("regs"),	PFSregs,	procfs_validregs },
    171  1.109     darcy 	{ DT_REG, N("fpregs"),	PFSfpregs,	procfs_validfpregs },
    172  1.109     darcy 	{ DT_REG, N("ctl"),	PFSctl,		NULL },
    173  1.136  christos 	{ DT_REG, N("stat"),	PFSstat,	procfs_validfile_linux },
    174  1.109     darcy 	{ DT_REG, N("status"),	PFSstatus,	NULL },
    175  1.136  christos 	{ DT_REG, N("note"),	PFSnote,	NULL },
    176  1.109     darcy 	{ DT_REG, N("notepg"),	PFSnotepg,	NULL },
    177  1.109     darcy 	{ DT_REG, N("map"),	PFSmap,		procfs_validmap },
    178  1.136  christos 	{ DT_REG, N("maps"),	PFSmaps,	procfs_validmap },
    179  1.109     darcy 	{ DT_REG, N("cmdline"), PFScmdline,	NULL },
    180  1.139     skrll 	{ DT_REG, N("exe"),	PFSexe,		procfs_validfile },
    181  1.126    atatat 	{ DT_LNK, N("cwd"),	PFScwd,		NULL },
    182  1.126    atatat 	{ DT_LNK, N("root"),	PFSchroot,	NULL },
    183  1.137  christos 	{ DT_LNK, N("emul"),	PFSemul,	NULL },
    184   1.86   thorpej #ifdef __HAVE_PROCFS_MACHDEP
    185   1.86   thorpej 	PROCFS_MACHDEP_NODETYPE_DEFNS
    186   1.86   thorpej #endif
    187    1.9       cgd #undef N
    188    1.1        pk };
    189   1.97  jdolecek static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
    190    1.1        pk 
    191   1.76      fvdl /*
    192   1.76      fvdl  * List of files in the root directory. Note: the validate function will
    193   1.76      fvdl  * be called with p == NULL for these ones.
    194   1.76      fvdl  */
    195   1.97  jdolecek static const struct proc_target proc_root_targets[] = {
    196   1.76      fvdl #define N(s) sizeof(s)-1, s
    197   1.76      fvdl 	/*	  name		    type	    validp */
    198  1.109     darcy 	{ DT_REG, N("meminfo"),     PFSmeminfo,        procfs_validfile_linux },
    199  1.109     darcy 	{ DT_REG, N("cpuinfo"),     PFScpuinfo,        procfs_validfile_linux },
    200  1.109     darcy 	{ DT_REG, N("uptime"),      PFSuptime,         procfs_validfile_linux },
    201  1.114  jdolecek 	{ DT_REG, N("mounts"),	    PFSmounts,	       procfs_validfile_linux },
    202  1.134      manu 	{ DT_REG, N("devices"),     PFSdevices,        procfs_validfile_linux },
    203   1.76      fvdl #undef N
    204   1.76      fvdl };
    205   1.97  jdolecek static const int nproc_root_targets =
    206   1.76      fvdl     sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
    207   1.76      fvdl 
    208  1.124   xtraeme int	procfs_lookup(void *);
    209   1.96  jdolecek #define	procfs_create	genfs_eopnotsupp
    210   1.96  jdolecek #define	procfs_mknod	genfs_eopnotsupp
    211  1.124   xtraeme int	procfs_open(void *);
    212  1.124   xtraeme int	procfs_close(void *);
    213  1.124   xtraeme int	procfs_access(void *);
    214  1.124   xtraeme int	procfs_getattr(void *);
    215  1.124   xtraeme int	procfs_setattr(void *);
    216   1.37  christos #define	procfs_read	procfs_rw
    217   1.37  christos #define	procfs_write	procfs_rw
    218   1.65  wrstuden #define	procfs_fcntl	genfs_fcntl
    219   1.57  matthias #define	procfs_ioctl	genfs_enoioctl
    220   1.42   mycroft #define	procfs_poll	genfs_poll
    221   1.53      fvdl #define procfs_revoke	genfs_revoke
    222   1.41   mycroft #define	procfs_fsync	genfs_nullop
    223   1.41   mycroft #define	procfs_seek	genfs_nullop
    224   1.96  jdolecek #define	procfs_remove	genfs_eopnotsupp
    225  1.124   xtraeme int	procfs_link(void *);
    226   1.96  jdolecek #define	procfs_rename	genfs_eopnotsupp
    227   1.96  jdolecek #define	procfs_mkdir	genfs_eopnotsupp
    228   1.96  jdolecek #define	procfs_rmdir	genfs_eopnotsupp
    229  1.124   xtraeme int	procfs_symlink(void *);
    230  1.124   xtraeme int	procfs_readdir(void *);
    231  1.124   xtraeme int	procfs_readlink(void *);
    232   1.41   mycroft #define	procfs_abortop	genfs_abortop
    233  1.124   xtraeme int	procfs_inactive(void *);
    234  1.124   xtraeme int	procfs_reclaim(void *);
    235   1.62  wrstuden #define	procfs_lock	genfs_lock
    236   1.62  wrstuden #define	procfs_unlock	genfs_unlock
    237   1.82       chs #define	procfs_bmap	genfs_badop
    238   1.41   mycroft #define	procfs_strategy	genfs_badop
    239  1.124   xtraeme int	procfs_print(void *);
    240  1.124   xtraeme int	procfs_pathconf(void *);
    241   1.62  wrstuden #define	procfs_islocked	genfs_islocked
    242   1.58    kleink #define	procfs_advlock	genfs_einval
    243   1.41   mycroft #define	procfs_bwrite	genfs_eopnotsupp
    244   1.87       chs #define procfs_putpages	genfs_null_putpages
    245   1.37  christos 
    246  1.124   xtraeme static int atoi(const char *, size_t);
    247   1.37  christos 
    248   1.37  christos /*
    249   1.37  christos  * procfs vnode operations.
    250   1.37  christos  */
    251  1.124   xtraeme int (**procfs_vnodeop_p)(void *);
    252   1.77  jdolecek const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
    253   1.37  christos 	{ &vop_default_desc, vn_default_error },
    254   1.37  christos 	{ &vop_lookup_desc, procfs_lookup },		/* lookup */
    255   1.37  christos 	{ &vop_create_desc, procfs_create },		/* create */
    256   1.37  christos 	{ &vop_mknod_desc, procfs_mknod },		/* mknod */
    257   1.37  christos 	{ &vop_open_desc, procfs_open },		/* open */
    258   1.37  christos 	{ &vop_close_desc, procfs_close },		/* close */
    259   1.37  christos 	{ &vop_access_desc, procfs_access },		/* access */
    260   1.37  christos 	{ &vop_getattr_desc, procfs_getattr },		/* getattr */
    261   1.37  christos 	{ &vop_setattr_desc, procfs_setattr },		/* setattr */
    262   1.37  christos 	{ &vop_read_desc, procfs_read },		/* read */
    263   1.37  christos 	{ &vop_write_desc, procfs_write },		/* write */
    264   1.65  wrstuden 	{ &vop_fcntl_desc, procfs_fcntl },		/* fcntl */
    265   1.37  christos 	{ &vop_ioctl_desc, procfs_ioctl },		/* ioctl */
    266   1.42   mycroft 	{ &vop_poll_desc, procfs_poll },		/* poll */
    267   1.53      fvdl 	{ &vop_revoke_desc, procfs_revoke },		/* revoke */
    268   1.37  christos 	{ &vop_fsync_desc, procfs_fsync },		/* fsync */
    269   1.37  christos 	{ &vop_seek_desc, procfs_seek },		/* seek */
    270   1.37  christos 	{ &vop_remove_desc, procfs_remove },		/* remove */
    271   1.37  christos 	{ &vop_link_desc, procfs_link },		/* link */
    272   1.37  christos 	{ &vop_rename_desc, procfs_rename },		/* rename */
    273   1.37  christos 	{ &vop_mkdir_desc, procfs_mkdir },		/* mkdir */
    274   1.37  christos 	{ &vop_rmdir_desc, procfs_rmdir },		/* rmdir */
    275   1.37  christos 	{ &vop_symlink_desc, procfs_symlink },		/* symlink */
    276   1.37  christos 	{ &vop_readdir_desc, procfs_readdir },		/* readdir */
    277   1.37  christos 	{ &vop_readlink_desc, procfs_readlink },	/* readlink */
    278   1.37  christos 	{ &vop_abortop_desc, procfs_abortop },		/* abortop */
    279   1.37  christos 	{ &vop_inactive_desc, procfs_inactive },	/* inactive */
    280   1.37  christos 	{ &vop_reclaim_desc, procfs_reclaim },		/* reclaim */
    281   1.37  christos 	{ &vop_lock_desc, procfs_lock },		/* lock */
    282   1.37  christos 	{ &vop_unlock_desc, procfs_unlock },		/* unlock */
    283   1.37  christos 	{ &vop_bmap_desc, procfs_bmap },		/* bmap */
    284   1.37  christos 	{ &vop_strategy_desc, procfs_strategy },	/* strategy */
    285   1.37  christos 	{ &vop_print_desc, procfs_print },		/* print */
    286   1.37  christos 	{ &vop_islocked_desc, procfs_islocked },	/* islocked */
    287   1.37  christos 	{ &vop_pathconf_desc, procfs_pathconf },	/* pathconf */
    288   1.37  christos 	{ &vop_advlock_desc, procfs_advlock },		/* advlock */
    289   1.87       chs 	{ &vop_putpages_desc, procfs_putpages },	/* putpages */
    290   1.82       chs 	{ NULL, NULL }
    291   1.37  christos };
    292   1.77  jdolecek const struct vnodeopv_desc procfs_vnodeop_opv_desc =
    293   1.37  christos 	{ &procfs_vnodeop_p, procfs_vnodeop_entries };
    294   1.37  christos /*
    295    1.9       cgd  * set things up for doing i/o on
    296    1.9       cgd  * the pfsnode (vp).  (vp) is locked
    297    1.9       cgd  * on entry, and should be left locked
    298    1.9       cgd  * on exit.
    299    1.1        pk  *
    300    1.9       cgd  * for procfs we don't need to do anything
    301    1.9       cgd  * in particular for i/o.  all that is done
    302    1.9       cgd  * is to support exclusive open on process
    303    1.9       cgd  * memory images.
    304    1.1        pk  */
    305   1.37  christos int
    306   1.37  christos procfs_open(v)
    307   1.37  christos 	void *v;
    308   1.37  christos {
    309   1.22   mycroft 	struct vop_open_args /* {
    310   1.22   mycroft 		struct vnode *a_vp;
    311   1.22   mycroft 		int  a_mode;
    312  1.130      elad 		kauth_cred_t a_cred;
    313  1.128  christos 		struct lwp *a_l;
    314   1.37  christos 	} */ *ap = v;
    315   1.21   mycroft 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    316  1.128  christos 	struct lwp *l1;
    317  1.128  christos 	struct proc *p2;
    318   1.88  christos 	int error;
    319   1.50   thorpej 
    320  1.145        ad 	if ((error = procfs_proc_lock(pfs->pfs_pid, &p2, ENOENT)) != 0)
    321  1.145        ad 		return error;
    322  1.145        ad 
    323  1.128  christos 	l1 = ap->a_l;				/* tracer */
    324  1.140      elad 
    325  1.144      elad #define	M2K(m)	(((m) & FREAD) && ((m) & FWRITE) ? \
    326  1.144      elad 		 KAUTH_REQ_PROCESS_CANPROCFS_RW : \
    327  1.144      elad 		 (m) & FWRITE ? KAUTH_REQ_PROCESS_CANPROCFS_WRITE : \
    328  1.144      elad 		 KAUTH_REQ_PROCESS_CANPROCFS_READ)
    329  1.144      elad 
    330  1.145        ad 	mutex_enter(&p2->p_mutex);
    331  1.144      elad 	error = kauth_authorize_process(l1->l_cred, KAUTH_PROCESS_CANPROCFS,
    332  1.144      elad 	    p2, pfs, KAUTH_ARG(M2K(ap->a_mode)), NULL);
    333  1.145        ad 	mutex_exit(&p2->p_mutex);
    334  1.144      elad 	if (error)
    335  1.144      elad 		return (error);
    336  1.144      elad 
    337  1.144      elad #undef M2K
    338  1.144      elad 
    339    1.9       cgd 	switch (pfs->pfs_type) {
    340  1.109     darcy 	case PFSmem:
    341   1.37  christos 		if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
    342  1.145        ad 		    ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))) {
    343  1.145        ad 			error = EBUSY;
    344  1.145        ad 			break;
    345  1.145        ad 		}
    346   1.50   thorpej 
    347  1.141      elad 		if (!proc_isunder(p2, l1))
    348  1.141      elad 			return (EPERM);
    349  1.141      elad 
    350   1.21   mycroft 		if (ap->a_mode & FWRITE)
    351   1.21   mycroft 			pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
    352    1.1        pk 
    353  1.144      elad 		break;
    354  1.144      elad 
    355  1.144      elad 	case PFSregs:
    356  1.144      elad 	case PFSfpregs:
    357  1.144      elad 		if (!proc_isunder(p2, l1))
    358  1.144      elad 			return (EPERM);
    359  1.144      elad 
    360  1.144      elad 		break;
    361    1.1        pk 
    362    1.9       cgd 	default:
    363    1.9       cgd 		break;
    364    1.9       cgd 	}
    365    1.1        pk 
    366  1.145        ad 	procfs_proc_unlock(p2);
    367  1.145        ad 	return (error);
    368    1.1        pk }
    369    1.1        pk 
    370    1.1        pk /*
    371    1.9       cgd  * close the pfsnode (vp) after doing i/o.
    372    1.9       cgd  * (vp) is not locked on entry or exit.
    373    1.9       cgd  *
    374    1.9       cgd  * nothing to do for procfs other than undo
    375    1.9       cgd  * any exclusive open flag (see _open above).
    376    1.1        pk  */
    377   1.37  christos int
    378   1.37  christos procfs_close(v)
    379   1.37  christos 	void *v;
    380   1.37  christos {
    381   1.22   mycroft 	struct vop_close_args /* {
    382   1.22   mycroft 		struct vnode *a_vp;
    383   1.22   mycroft 		int  a_fflag;
    384  1.130      elad 		kauth_cred_t a_cred;
    385  1.128  christos 		struct lwp *a_l;
    386   1.37  christos 	} */ *ap = v;
    387   1.21   mycroft 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    388    1.1        pk 
    389    1.9       cgd 	switch (pfs->pfs_type) {
    390  1.109     darcy 	case PFSmem:
    391   1.21   mycroft 		if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
    392    1.9       cgd 			pfs->pfs_flags &= ~(FWRITE|O_EXCL);
    393    1.9       cgd 		break;
    394   1.46   mycroft 
    395   1.46   mycroft 	default:
    396   1.37  christos 		break;
    397    1.9       cgd 	}
    398    1.1        pk 
    399    1.1        pk 	return (0);
    400    1.1        pk }
    401    1.1        pk 
    402    1.1        pk /*
    403    1.9       cgd  * _inactive is called when the pfsnode
    404    1.9       cgd  * is vrele'd and the reference count goes
    405    1.9       cgd  * to zero.  (vp) will be on the vnode free
    406    1.9       cgd  * list, so to get it back vget() must be
    407    1.9       cgd  * used.
    408    1.9       cgd  *
    409    1.9       cgd  * for procfs, check if the process is still
    410    1.9       cgd  * alive and if it isn't then just throw away
    411    1.9       cgd  * the vnode by calling vgone().  this may
    412    1.9       cgd  * be overkill and a waste of time since the
    413    1.9       cgd  * chances are that the process will still be
    414  1.145        ad  * there.
    415    1.9       cgd  *
    416   1.62  wrstuden  * (vp) is locked on entry, but must be unlocked on exit.
    417    1.1        pk  */
    418   1.37  christos int
    419   1.37  christos procfs_inactive(v)
    420   1.37  christos 	void *v;
    421   1.37  christos {
    422   1.22   mycroft 	struct vop_inactive_args /* {
    423   1.22   mycroft 		struct vnode *a_vp;
    424   1.53      fvdl 		struct proc *a_p;
    425   1.37  christos 	} */ *ap = v;
    426  1.122  christos 	struct vnode *vp = ap->a_vp;
    427  1.122  christos 	struct pfsnode *pfs = VTOPFS(vp);
    428  1.145        ad 	struct proc *p;
    429  1.145        ad 	int error;
    430    1.1        pk 
    431  1.122  christos 	VOP_UNLOCK(vp, 0);
    432  1.145        ad 
    433  1.145        ad 	error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH);
    434  1.145        ad 	if (error != 0 && (vp->v_flag & VXLOCK) == 0)
    435  1.122  christos 		vgone(vp);
    436  1.145        ad 	else
    437  1.145        ad 		procfs_proc_unlock(p);
    438    1.5        pk 
    439    1.9       cgd 	return (0);
    440    1.1        pk }
    441    1.1        pk 
    442    1.1        pk /*
    443    1.9       cgd  * _reclaim is called when getnewvnode()
    444    1.9       cgd  * wants to make use of an entry on the vnode
    445    1.9       cgd  * free list.  at this time the filesystem needs
    446    1.9       cgd  * to free any private data and remove the node
    447    1.9       cgd  * from any private lists.
    448    1.1        pk  */
    449   1.37  christos int
    450   1.37  christos procfs_reclaim(v)
    451   1.37  christos 	void *v;
    452   1.37  christos {
    453   1.22   mycroft 	struct vop_reclaim_args /* {
    454   1.22   mycroft 		struct vnode *a_vp;
    455   1.37  christos 	} */ *ap = v;
    456   1.21   mycroft 
    457   1.21   mycroft 	return (procfs_freevp(ap->a_vp));
    458   1.21   mycroft }
    459   1.21   mycroft 
    460   1.21   mycroft /*
    461   1.21   mycroft  * Return POSIX pathconf information applicable to special devices.
    462   1.21   mycroft  */
    463   1.37  christos int
    464   1.37  christos procfs_pathconf(v)
    465   1.37  christos 	void *v;
    466   1.37  christos {
    467   1.21   mycroft 	struct vop_pathconf_args /* {
    468   1.21   mycroft 		struct vnode *a_vp;
    469   1.21   mycroft 		int a_name;
    470   1.26       cgd 		register_t *a_retval;
    471   1.37  christos 	} */ *ap = v;
    472    1.1        pk 
    473   1.21   mycroft 	switch (ap->a_name) {
    474   1.21   mycroft 	case _PC_LINK_MAX:
    475   1.21   mycroft 		*ap->a_retval = LINK_MAX;
    476   1.21   mycroft 		return (0);
    477   1.21   mycroft 	case _PC_MAX_CANON:
    478   1.21   mycroft 		*ap->a_retval = MAX_CANON;
    479   1.21   mycroft 		return (0);
    480   1.21   mycroft 	case _PC_MAX_INPUT:
    481   1.21   mycroft 		*ap->a_retval = MAX_INPUT;
    482   1.21   mycroft 		return (0);
    483   1.21   mycroft 	case _PC_PIPE_BUF:
    484   1.21   mycroft 		*ap->a_retval = PIPE_BUF;
    485   1.21   mycroft 		return (0);
    486   1.21   mycroft 	case _PC_CHOWN_RESTRICTED:
    487   1.21   mycroft 		*ap->a_retval = 1;
    488   1.21   mycroft 		return (0);
    489   1.21   mycroft 	case _PC_VDISABLE:
    490   1.21   mycroft 		*ap->a_retval = _POSIX_VDISABLE;
    491   1.55    kleink 		return (0);
    492   1.55    kleink 	case _PC_SYNC_IO:
    493   1.55    kleink 		*ap->a_retval = 1;
    494   1.21   mycroft 		return (0);
    495   1.21   mycroft 	default:
    496   1.21   mycroft 		return (EINVAL);
    497   1.21   mycroft 	}
    498   1.21   mycroft 	/* NOTREACHED */
    499    1.1        pk }
    500    1.1        pk 
    501    1.1        pk /*
    502    1.9       cgd  * _print is used for debugging.
    503    1.9       cgd  * just print a readable description
    504    1.9       cgd  * of (vp).
    505    1.1        pk  */
    506   1.37  christos int
    507   1.37  christos procfs_print(v)
    508   1.37  christos 	void *v;
    509   1.37  christos {
    510   1.22   mycroft 	struct vop_print_args /* {
    511   1.22   mycroft 		struct vnode *a_vp;
    512   1.37  christos 	} */ *ap = v;
    513   1.21   mycroft 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    514    1.5        pk 
    515   1.44  christos 	printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
    516   1.21   mycroft 	    pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
    517   1.37  christos 	return 0;
    518   1.36   mycroft }
    519   1.36   mycroft 
    520   1.36   mycroft int
    521  1.121     perry procfs_link(v)
    522   1.37  christos 	void *v;
    523   1.37  christos {
    524   1.36   mycroft 	struct vop_link_args /* {
    525   1.36   mycroft 		struct vnode *a_dvp;
    526  1.121     perry 		struct vnode *a_vp;
    527   1.36   mycroft 		struct componentname *a_cnp;
    528   1.37  christos 	} */ *ap = v;
    529  1.121     perry 
    530   1.36   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    531   1.36   mycroft 	vput(ap->a_dvp);
    532   1.36   mycroft 	return (EROFS);
    533   1.36   mycroft }
    534   1.36   mycroft 
    535   1.36   mycroft int
    536   1.37  christos procfs_symlink(v)
    537   1.37  christos 	void *v;
    538   1.37  christos {
    539   1.36   mycroft 	struct vop_symlink_args /* {
    540   1.36   mycroft 		struct vnode *a_dvp;
    541   1.36   mycroft 		struct vnode **a_vpp;
    542   1.36   mycroft 		struct componentname *a_cnp;
    543   1.36   mycroft 		struct vattr *a_vap;
    544   1.36   mycroft 		char *a_target;
    545   1.37  christos 	} */ *ap = v;
    546  1.121     perry 
    547   1.36   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    548   1.36   mycroft 	vput(ap->a_dvp);
    549   1.36   mycroft 	return (EROFS);
    550    1.1        pk }
    551    1.1        pk 
    552    1.1        pk /*
    553  1.126    atatat  * Works out the path to (and vnode of) the target process's current
    554  1.126    atatat  * working directory or chroot.  If the caller is in a chroot and
    555  1.126    atatat  * can't "reach" the target's cwd or root (or some other error
    556  1.126    atatat  * occurs), a "/" is returned for the path and a NULL pointer is
    557  1.126    atatat  * returned for the vnode.
    558  1.126    atatat  */
    559  1.126    atatat static struct vnode *
    560  1.128  christos procfs_dir(pfstype t, struct lwp *caller, struct proc *target,
    561  1.126    atatat 	   char **bpp, char *path, int len)
    562  1.126    atatat {
    563  1.128  christos 	struct vnode *vp, *rvp = caller->l_proc->p_cwdi->cwdi_rdir;
    564  1.126    atatat 	char *bp;
    565  1.126    atatat 
    566  1.145        ad 	LOCK_ASSERT(mutex_owned(&target->p_mutex));
    567  1.145        ad 
    568  1.126    atatat 	bp = bpp ? *bpp : NULL;
    569  1.126    atatat 
    570  1.126    atatat 	switch (t) {
    571  1.126    atatat 	case PFScwd:
    572  1.126    atatat 		vp = target->p_cwdi->cwdi_cdir;
    573  1.126    atatat 		break;
    574  1.126    atatat 	case PFSchroot:
    575  1.126    atatat 		vp = target->p_cwdi->cwdi_rdir;
    576  1.126    atatat 		break;
    577  1.136  christos 	case PFSexe:
    578  1.136  christos 		rvp = rootvnode;
    579  1.136  christos 		vp = target->p_textvp;
    580  1.136  christos 		break;
    581  1.126    atatat 	default:
    582  1.126    atatat 		return (NULL);
    583  1.126    atatat 	}
    584  1.126    atatat 
    585  1.126    atatat 	if (rvp == NULL)
    586  1.126    atatat 		rvp = rootvnode;
    587  1.145        ad 	mutex_exit(&target->p_mutex);	/* XXXSMP */
    588  1.126    atatat 	if (vp == NULL || getcwd_common(vp, rvp, bp ? &bp : NULL, path,
    589  1.126    atatat 	    len / 2, 0, caller) != 0) {
    590  1.126    atatat 		vp = NULL;
    591  1.126    atatat 		if (bpp) {
    592  1.126    atatat 			bp = *bpp;
    593  1.126    atatat 			*--bp = '/';
    594  1.126    atatat 		}
    595  1.126    atatat 	}
    596  1.145        ad 	mutex_enter(&target->p_mutex);	/* XXXSMP */
    597  1.126    atatat 
    598  1.126    atatat 	if (bpp)
    599  1.126    atatat 		*bpp = bp;
    600  1.126    atatat 
    601  1.126    atatat 	return (vp);
    602  1.126    atatat }
    603  1.126    atatat 
    604  1.126    atatat /*
    605    1.9       cgd  * Invent attributes for pfsnode (vp) and store
    606    1.9       cgd  * them in (vap).
    607    1.9       cgd  * Directories lengths are returned as zero since
    608    1.9       cgd  * any real length would require the genuine size
    609    1.9       cgd  * to be computed, and nothing cares anyway.
    610    1.9       cgd  *
    611    1.9       cgd  * this is relatively minimal for procfs.
    612    1.1        pk  */
    613   1.37  christos int
    614   1.37  christos procfs_getattr(v)
    615   1.37  christos 	void *v;
    616   1.37  christos {
    617   1.22   mycroft 	struct vop_getattr_args /* {
    618   1.22   mycroft 		struct vnode *a_vp;
    619   1.22   mycroft 		struct vattr *a_vap;
    620  1.130      elad 		kauth_cred_t a_cred;
    621  1.128  christos 		struct lwp *a_l;
    622   1.37  christos 	} */ *ap = v;
    623   1.21   mycroft 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    624   1.21   mycroft 	struct vattr *vap = ap->a_vap;
    625    1.1        pk 	struct proc *procp;
    626  1.145        ad 	char *path;
    627    1.9       cgd 	int error;
    628    1.1        pk 
    629   1.21   mycroft 	/* first check the process still exists */
    630   1.21   mycroft 	switch (pfs->pfs_type) {
    631  1.109     darcy 	case PFSroot:
    632  1.109     darcy 	case PFScurproc:
    633  1.109     darcy 	case PFSself:
    634  1.145        ad 		procp = NULL;
    635  1.145        ad 		break;
    636  1.145        ad 
    637  1.145        ad 	default:
    638  1.145        ad 		error = procfs_proc_lock(pfs->pfs_pid, &procp, ENOENT);
    639  1.145        ad 		if (error != 0)
    640  1.145        ad 			return (error);
    641  1.145        ad 		break;
    642  1.145        ad 	}
    643  1.145        ad 
    644  1.145        ad 	switch (pfs->pfs_type) {
    645  1.145        ad 	case PFScwd:
    646  1.145        ad 	case PFSchroot:
    647  1.145        ad 	case PFSexe:
    648  1.145        ad 		MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP,
    649  1.145        ad 		    M_WAITOK|M_CANFAIL);
    650  1.146        ad 		if (path == NULL && procp != NULL) {
    651  1.145        ad 			procfs_proc_unlock(procp);
    652  1.145        ad 			return (ENOMEM);
    653  1.145        ad 		}
    654   1.21   mycroft 		break;
    655   1.21   mycroft 
    656   1.21   mycroft 	default:
    657  1.145        ad 		path = NULL;
    658   1.46   mycroft 		break;
    659   1.21   mycroft 	}
    660   1.21   mycroft 
    661  1.125      elad 	if (procp != NULL) {
    662  1.145        ad 		mutex_enter(&procp->p_mutex);
    663  1.146        ad 		error = kauth_authorize_process(kauth_cred_get(),
    664  1.146        ad 		    KAUTH_PROCESS_CANSEE, procp, NULL, NULL, NULL);
    665  1.146        ad 		mutex_exit(&procp->p_mutex);
    666  1.146        ad 		if (error != 0) {
    667  1.145        ad 		    	procfs_proc_unlock(procp);
    668  1.145        ad 		    	if (path != NULL)
    669  1.145        ad 		    		free(path, M_TEMP);
    670  1.125      elad 			return (ENOENT);
    671  1.145        ad 		}
    672  1.125      elad 	}
    673  1.125      elad 
    674   1.21   mycroft 	error = 0;
    675   1.21   mycroft 
    676    1.9       cgd 	/* start by zeroing out the attributes */
    677    1.1        pk 	VATTR_NULL(vap);
    678    1.9       cgd 
    679    1.9       cgd 	/* next do all the common fields */
    680   1.21   mycroft 	vap->va_type = ap->a_vp->v_type;
    681    1.9       cgd 	vap->va_mode = pfs->pfs_mode;
    682    1.9       cgd 	vap->va_fileid = pfs->pfs_fileno;
    683    1.9       cgd 	vap->va_flags = 0;
    684   1.19       cgd 	vap->va_blocksize = PAGE_SIZE;
    685    1.9       cgd 
    686    1.9       cgd 	/*
    687  1.131    kardel 	 * Make all times be current TOD.
    688   1.90    simonb 	 *
    689    1.9       cgd 	 * It would be possible to get the process start
    690  1.126    atatat 	 * time from the p_stats structure, but there's
    691    1.9       cgd 	 * no "file creation" time stamp anyway, and the
    692  1.126    atatat 	 * p_stats structure is not addressable if u. gets
    693    1.9       cgd 	 * swapped out for that process.
    694    1.9       cgd 	 */
    695  1.131    kardel 	getnanotime(&vap->va_ctime);
    696    1.9       cgd 	vap->va_atime = vap->va_mtime = vap->va_ctime;
    697  1.126    atatat 	if (procp)
    698  1.126    atatat 		TIMEVAL_TO_TIMESPEC(&procp->p_stats->p_start,
    699  1.126    atatat 		    &vap->va_birthtime);
    700  1.126    atatat 	else
    701  1.131    kardel 		getnanotime(&vap->va_birthtime);
    702    1.9       cgd 
    703   1.21   mycroft 	switch (pfs->pfs_type) {
    704  1.109     darcy 	case PFSmem:
    705  1.109     darcy 	case PFSregs:
    706  1.109     darcy 	case PFSfpregs:
    707   1.86   thorpej #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    708   1.86   thorpej 	PROCFS_MACHDEP_PROTECT_CASES
    709   1.86   thorpej #endif
    710   1.47   mycroft 		/*
    711   1.47   mycroft 		 * If the process has exercised some setuid or setgid
    712   1.47   mycroft 		 * privilege, then rip away read/write permission so
    713   1.47   mycroft 		 * that only root can gain access.
    714   1.47   mycroft 		 */
    715  1.149     pavel 		if (procp->p_flag & PK_SUGID)
    716   1.47   mycroft 			vap->va_mode &= ~(S_IRUSR|S_IWUSR);
    717   1.46   mycroft 		/* FALLTHROUGH */
    718  1.109     darcy 	case PFSctl:
    719  1.109     darcy 	case PFSstatus:
    720  1.109     darcy 	case PFSstat:
    721  1.109     darcy 	case PFSnote:
    722  1.109     darcy 	case PFSnotepg:
    723  1.109     darcy 	case PFSmap:
    724  1.109     darcy 	case PFSmaps:
    725  1.109     darcy 	case PFScmdline:
    726  1.137  christos 	case PFSemul:
    727   1.12        ws 		vap->va_nlink = 1;
    728  1.130      elad 		vap->va_uid = kauth_cred_geteuid(procp->p_cred);
    729  1.130      elad 		vap->va_gid = kauth_cred_getegid(procp->p_cred);
    730   1.21   mycroft 		break;
    731  1.109     darcy 	case PFSmeminfo:
    732  1.134      manu 	case PFSdevices:
    733  1.109     darcy 	case PFScpuinfo:
    734  1.109     darcy 	case PFSuptime:
    735  1.114  jdolecek 	case PFSmounts:
    736   1.76      fvdl 		vap->va_nlink = 1;
    737   1.76      fvdl 		vap->va_uid = vap->va_gid = 0;
    738   1.76      fvdl 		break;
    739   1.46   mycroft 
    740   1.46   mycroft 	default:
    741   1.37  christos 		break;
    742   1.12        ws 	}
    743   1.12        ws 
    744    1.9       cgd 	/*
    745    1.9       cgd 	 * now do the object specific fields
    746    1.9       cgd 	 *
    747    1.9       cgd 	 * The size could be set from struct reg, but it's hardly
    748    1.9       cgd 	 * worth the trouble, and it puts some (potentially) machine
    749    1.9       cgd 	 * dependent data into this machine-independent code.  If it
    750    1.9       cgd 	 * becomes important then this function should break out into
    751    1.9       cgd 	 * a per-file stat function in the corresponding .c file.
    752    1.9       cgd 	 */
    753    1.1        pk 
    754    1.9       cgd 	switch (pfs->pfs_type) {
    755  1.109     darcy 	case PFSroot:
    756   1.21   mycroft 		/*
    757   1.21   mycroft 		 * Set nlink to 1 to tell fts(3) we don't actually know.
    758   1.21   mycroft 		 */
    759   1.21   mycroft 		vap->va_nlink = 1;
    760   1.21   mycroft 		vap->va_uid = 0;
    761   1.21   mycroft 		vap->va_gid = 0;
    762   1.46   mycroft 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    763   1.21   mycroft 		break;
    764   1.21   mycroft 
    765  1.129  christos 	case PFSself:
    766  1.109     darcy 	case PFScurproc: {
    767  1.123  christos 		char bf[16];		/* should be enough */
    768   1.21   mycroft 		vap->va_nlink = 1;
    769   1.21   mycroft 		vap->va_uid = 0;
    770   1.21   mycroft 		vap->va_gid = 0;
    771   1.46   mycroft 		vap->va_bytes = vap->va_size =
    772  1.123  christos 		    snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
    773    1.9       cgd 		break;
    774   1.21   mycroft 	}
    775    1.9       cgd 
    776  1.109     darcy 	case PFSfd:
    777   1.91  christos 		if (pfs->pfs_fd != -1) {
    778   1.91  christos 			struct file *fp;
    779   1.98  jdolecek 
    780  1.145        ad 			fp = fd_getfile(procp->p_fd, pfs->pfs_fd);
    781  1.145        ad 			if (fp == NULL) {
    782  1.145        ad 				error = EBADF;
    783  1.145        ad 				break;
    784  1.145        ad 			}
    785   1.98  jdolecek 			FILE_USE(fp);
    786   1.91  christos 			vap->va_nlink = 1;
    787  1.130      elad 			vap->va_uid = kauth_cred_geteuid(fp->f_cred);
    788  1.130      elad 			vap->va_gid = kauth_cred_getegid(fp->f_cred);
    789   1.91  christos 			switch (fp->f_type) {
    790   1.91  christos 			case DTYPE_VNODE:
    791   1.91  christos 				vap->va_bytes = vap->va_size =
    792   1.91  christos 				    ((struct vnode *)fp->f_data)->v_size;
    793   1.91  christos 				break;
    794   1.91  christos 			default:
    795   1.91  christos 				vap->va_bytes = vap->va_size = 0;
    796   1.91  christos 				break;
    797   1.91  christos 			}
    798  1.145        ad 			FILE_UNUSE(fp, curlwp);
    799   1.91  christos 			break;
    800   1.91  christos 		}
    801   1.91  christos 		/*FALLTHROUGH*/
    802  1.109     darcy 	case PFSproc:
    803    1.9       cgd 		vap->va_nlink = 2;
    804  1.130      elad 		vap->va_uid = kauth_cred_geteuid(procp->p_cred);
    805  1.130      elad 		vap->va_gid = kauth_cred_getegid(procp->p_cred);
    806   1.46   mycroft 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    807   1.21   mycroft 		break;
    808   1.21   mycroft 
    809  1.109     darcy 	case PFSfile:
    810   1.21   mycroft 		error = EOPNOTSUPP;
    811    1.9       cgd 		break;
    812    1.9       cgd 
    813  1.109     darcy 	case PFSmem:
    814    1.9       cgd 		vap->va_bytes = vap->va_size =
    815    1.9       cgd 			ctob(procp->p_vmspace->vm_tsize +
    816    1.9       cgd 				    procp->p_vmspace->vm_dsize +
    817    1.9       cgd 				    procp->p_vmspace->vm_ssize);
    818    1.9       cgd 		break;
    819    1.9       cgd 
    820   1.18       cgd #if defined(PT_GETREGS) || defined(PT_SETREGS)
    821  1.109     darcy 	case PFSregs:
    822   1.11       cgd 		vap->va_bytes = vap->va_size = sizeof(struct reg);
    823   1.18       cgd 		break;
    824   1.14       cgd #endif
    825   1.14       cgd 
    826   1.18       cgd #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
    827  1.109     darcy 	case PFSfpregs:
    828   1.14       cgd 		vap->va_bytes = vap->va_size = sizeof(struct fpreg);
    829   1.18       cgd 		break;
    830   1.14       cgd #endif
    831   1.12        ws 
    832  1.109     darcy 	case PFSctl:
    833  1.109     darcy 	case PFSstatus:
    834  1.109     darcy 	case PFSstat:
    835  1.109     darcy 	case PFSnote:
    836  1.109     darcy 	case PFSnotepg:
    837  1.109     darcy 	case PFScmdline:
    838  1.109     darcy 	case PFSmeminfo:
    839  1.134      manu 	case PFSdevices:
    840  1.109     darcy 	case PFScpuinfo:
    841  1.109     darcy 	case PFSuptime:
    842  1.114  jdolecek 	case PFSmounts:
    843   1.46   mycroft 		vap->va_bytes = vap->va_size = 0;
    844   1.79      fvdl 		break;
    845  1.109     darcy 	case PFSmap:
    846  1.109     darcy 	case PFSmaps:
    847   1.79      fvdl 		/*
    848   1.79      fvdl 		 * Advise a larger blocksize for the map files, so that
    849   1.79      fvdl 		 * they may be read in one pass.
    850   1.79      fvdl 		 */
    851   1.80      fvdl 		vap->va_blocksize = 4 * PAGE_SIZE;
    852   1.83       chs 		vap->va_bytes = vap->va_size = 0;
    853    1.9       cgd 		break;
    854   1.86   thorpej 
    855  1.126    atatat 	case PFScwd:
    856  1.136  christos 	case PFSchroot:
    857  1.136  christos 	case PFSexe: {
    858  1.145        ad 		char *bp;
    859  1.126    atatat 
    860  1.126    atatat 		vap->va_nlink = 1;
    861  1.126    atatat 		vap->va_uid = 0;
    862  1.126    atatat 		vap->va_gid = 0;
    863  1.126    atatat 		bp = path + MAXPATHLEN;
    864  1.126    atatat 		*--bp = '\0';
    865  1.147        ad 		mutex_enter(&procp->p_mutex);
    866  1.128  christos 		(void)procfs_dir(pfs->pfs_type, curlwp, procp, &bp, path,
    867  1.126    atatat 		     MAXPATHLEN);
    868  1.147        ad 		mutex_exit(&procp->p_mutex);
    869  1.126    atatat 		vap->va_bytes = vap->va_size = strlen(bp);
    870  1.126    atatat 		break;
    871  1.126    atatat 	}
    872  1.126    atatat 
    873  1.137  christos 	case PFSemul:
    874  1.137  christos 		vap->va_bytes = vap->va_size = strlen(procp->p_emul->e_name);
    875  1.137  christos 		break;
    876  1.137  christos 
    877   1.86   thorpej #ifdef __HAVE_PROCFS_MACHDEP
    878   1.86   thorpej 	PROCFS_MACHDEP_NODETYPE_CASES
    879   1.86   thorpej 		error = procfs_machdep_getattr(ap->a_vp, vap, procp);
    880   1.86   thorpej 		break;
    881   1.86   thorpej #endif
    882    1.1        pk 
    883    1.9       cgd 	default:
    884   1.21   mycroft 		panic("procfs_getattr");
    885    1.1        pk 	}
    886    1.1        pk 
    887  1.146        ad 	if (procp != NULL)
    888  1.145        ad 		procfs_proc_unlock(procp);
    889  1.145        ad 	if (path != NULL)
    890  1.145        ad 		free(path, M_TEMP);
    891  1.145        ad 
    892    1.9       cgd 	return (error);
    893    1.1        pk }
    894    1.1        pk 
    895   1.37  christos /*ARGSUSED*/
    896   1.37  christos int
    897  1.138  christos procfs_setattr(void *v)
    898    1.5        pk {
    899    1.5        pk 	/*
    900    1.9       cgd 	 * just fake out attribute setting
    901    1.9       cgd 	 * it's not good to generate an error
    902    1.9       cgd 	 * return, otherwise things like creat()
    903    1.9       cgd 	 * will fail when they try to set the
    904    1.9       cgd 	 * file length to 0.  worse, this means
    905    1.9       cgd 	 * that echo $note > /proc/$pid/note will fail.
    906    1.5        pk 	 */
    907    1.5        pk 
    908    1.9       cgd 	return (0);
    909    1.5        pk }
    910    1.5        pk 
    911    1.9       cgd /*
    912    1.9       cgd  * implement access checking.
    913    1.9       cgd  *
    914    1.9       cgd  * actually, the check for super-user is slightly
    915    1.9       cgd  * broken since it will allow read access to write-only
    916    1.9       cgd  * objects.  this doesn't cause any particular trouble
    917    1.9       cgd  * but does mean that the i/o entry points need to check
    918    1.9       cgd  * that the operation really does make sense.
    919    1.9       cgd  */
    920   1.37  christos int
    921   1.37  christos procfs_access(v)
    922   1.37  christos 	void *v;
    923   1.37  christos {
    924   1.22   mycroft 	struct vop_access_args /* {
    925   1.22   mycroft 		struct vnode *a_vp;
    926   1.22   mycroft 		int a_mode;
    927  1.130      elad 		kauth_cred_t a_cred;
    928  1.128  christos 		struct lwp *a_l;
    929   1.37  christos 	} */ *ap = v;
    930   1.31   mycroft 	struct vattr va;
    931    1.1        pk 	int error;
    932    1.1        pk 
    933  1.128  christos 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_l)) != 0)
    934    1.1        pk 		return (error);
    935    1.9       cgd 
    936   1.49   mycroft 	return (vaccess(va.va_type, va.va_mode,
    937   1.49   mycroft 	    va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
    938    1.1        pk }
    939    1.1        pk 
    940    1.1        pk /*
    941    1.9       cgd  * lookup.  this is incredibly complicated in the
    942    1.9       cgd  * general case, however for most pseudo-filesystems
    943    1.9       cgd  * very little needs to be done.
    944    1.9       cgd  *
    945   1.62  wrstuden  * Locking isn't hard here, just poorly documented.
    946   1.62  wrstuden  *
    947  1.121     perry  * If we're looking up ".", just vref the parent & return it.
    948   1.62  wrstuden  *
    949   1.62  wrstuden  * If we're looking up "..", unlock the parent, and lock "..". If everything
    950   1.62  wrstuden  * went ok, and we're on the last component and the caller requested the
    951   1.62  wrstuden  * parent locked, try to re-lock the parent. We do this to prevent lock
    952   1.62  wrstuden  * races.
    953   1.62  wrstuden  *
    954   1.62  wrstuden  * For anything else, get the needed node. Then unlock the parent if not
    955   1.62  wrstuden  * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
    956   1.62  wrstuden  * parent in the .. case).
    957   1.62  wrstuden  *
    958   1.62  wrstuden  * We try to exit with the parent locked in error cases.
    959    1.9       cgd  */
    960   1.37  christos int
    961   1.37  christos procfs_lookup(v)
    962   1.37  christos 	void *v;
    963   1.37  christos {
    964   1.22   mycroft 	struct vop_lookup_args /* {
    965   1.22   mycroft 		struct vnode * a_dvp;
    966   1.22   mycroft 		struct vnode ** a_vpp;
    967   1.22   mycroft 		struct componentname * a_cnp;
    968   1.37  christos 	} */ *ap = v;
    969   1.21   mycroft 	struct componentname *cnp = ap->a_cnp;
    970   1.21   mycroft 	struct vnode **vpp = ap->a_vpp;
    971   1.21   mycroft 	struct vnode *dvp = ap->a_dvp;
    972   1.45       cgd 	const char *pname = cnp->cn_nameptr;
    973   1.78  jdolecek 	const struct proc_target *pt = NULL;
    974   1.32   mycroft 	struct vnode *fvp;
    975  1.146        ad 	pid_t pid, vnpid;
    976    1.9       cgd 	struct pfsnode *pfs;
    977   1.76      fvdl 	struct proc *p = NULL;
    978  1.128  christos 	struct lwp *l = NULL;
    979  1.146        ad 	int i, error;
    980  1.146        ad 	pfstype type;
    981    1.1        pk 
    982   1.32   mycroft 	*vpp = NULL;
    983   1.32   mycroft 
    984   1.32   mycroft 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    985   1.32   mycroft 		return (EROFS);
    986   1.32   mycroft 
    987   1.21   mycroft 	if (cnp->cn_namelen == 1 && *pname == '.') {
    988   1.21   mycroft 		*vpp = dvp;
    989    1.9       cgd 		VREF(dvp);
    990    1.9       cgd 		return (0);
    991    1.9       cgd 	}
    992    1.1        pk 
    993    1.9       cgd 	pfs = VTOPFS(dvp);
    994    1.9       cgd 	switch (pfs->pfs_type) {
    995  1.109     darcy 	case PFSroot:
    996   1.62  wrstuden 		/*
    997   1.62  wrstuden 		 * Shouldn't get here with .. in the root node.
    998   1.62  wrstuden 		 */
    999  1.121     perry 		if (cnp->cn_flags & ISDOTDOT)
   1000    1.9       cgd 			return (EIO);
   1001    1.9       cgd 
   1002   1.76      fvdl 		for (i = 0; i < nproc_root_targets; i++) {
   1003   1.76      fvdl 			pt = &proc_root_targets[i];
   1004  1.150     pooka 			/*
   1005  1.150     pooka 			 * check for node match.  proc is always NULL here,
   1006  1.150     pooka 			 * so call pt_valid with constant NULL lwp.
   1007  1.150     pooka 			 */
   1008   1.76      fvdl 			if (cnp->cn_namelen == pt->pt_namlen &&
   1009   1.76      fvdl 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
   1010  1.150     pooka 			    (pt->pt_valid == NULL ||
   1011  1.150     pooka 			     (*pt->pt_valid)(NULL, dvp->v_mount)))
   1012   1.76      fvdl 				break;
   1013   1.76      fvdl 		}
   1014   1.76      fvdl 
   1015   1.76      fvdl 		if (i != nproc_root_targets) {
   1016   1.76      fvdl 			error = procfs_allocvp(dvp->v_mount, vpp, 0,
   1017  1.145        ad 			    pt->pt_pfstype, -1, NULL);
   1018   1.76      fvdl 			return (error);
   1019   1.76      fvdl 		}
   1020   1.76      fvdl 
   1021  1.146        ad 		if (CNEQ(cnp, "curproc", 7)) {
   1022  1.146        ad 			pid = curproc->p_pid;
   1023  1.146        ad 			vnpid = 0;
   1024  1.146        ad 			type = PFScurproc;
   1025  1.146        ad 		} else if (CNEQ(cnp, "self", 4)) {
   1026  1.146        ad 			pid = curproc->p_pid;
   1027  1.146        ad 			vnpid = 0;
   1028  1.146        ad 			type = PFSself;
   1029  1.146        ad 		} else {
   1030  1.146        ad 			pid = (pid_t)atoi(pname, cnp->cn_namelen);
   1031  1.146        ad 			vnpid = pid;
   1032  1.146        ad 			type = PFSproc;
   1033  1.146        ad 		}
   1034    1.9       cgd 
   1035  1.145        ad 		if (procfs_proc_lock(pid, &p, ESRCH) != 0)
   1036   1.32   mycroft 			break;
   1037  1.146        ad 		error = procfs_allocvp(dvp->v_mount, vpp, vnpid, type, -1, p);
   1038  1.145        ad 		procfs_proc_unlock(p);
   1039   1.62  wrstuden 		return (error);
   1040    1.9       cgd 
   1041  1.109     darcy 	case PFSproc:
   1042   1.62  wrstuden 		/*
   1043   1.62  wrstuden 		 * do the .. dance. We unlock the directory, and then
   1044   1.62  wrstuden 		 * get the root dir. That will automatically return ..
   1045   1.62  wrstuden 		 * locked. Then if the caller wanted dvp locked, we
   1046   1.62  wrstuden 		 * re-lock.
   1047   1.62  wrstuden 		 */
   1048   1.62  wrstuden 		if (cnp->cn_flags & ISDOTDOT) {
   1049   1.62  wrstuden 			VOP_UNLOCK(dvp, 0);
   1050  1.105   thorpej 			error = procfs_root(dvp->v_mount, vpp);
   1051  1.143       chs 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
   1052   1.62  wrstuden 			return (error);
   1053   1.62  wrstuden 		}
   1054    1.1        pk 
   1055  1.145        ad 		if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
   1056   1.32   mycroft 			break;
   1057   1.32   mycroft 
   1058   1.32   mycroft 		for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
   1059  1.151     pooka 			struct lwp *plwp;
   1060  1.151     pooka 			int found;
   1061  1.151     pooka 
   1062  1.151     pooka 			mutex_enter(&p->p_smutex);
   1063  1.151     pooka 			plwp = proc_representative_lwp(p, NULL, 1);
   1064  1.151     pooka 			lwp_addref(plwp);
   1065  1.151     pooka 			mutex_exit(&p->p_smutex);
   1066  1.151     pooka 			found = cnp->cn_namelen == pt->pt_namlen &&
   1067   1.56     perry 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
   1068  1.151     pooka 			    (pt->pt_valid == NULL
   1069  1.151     pooka 			      || (*pt->pt_valid)(plwp, dvp->v_mount));
   1070  1.151     pooka 			lwp_delref(plwp);
   1071  1.151     pooka 			if (found)
   1072  1.145        ad 				break;
   1073    1.9       cgd 		}
   1074  1.146        ad 		if (i == nproc_targets) {
   1075  1.146        ad 			procfs_proc_unlock(p);
   1076  1.145        ad 			break;
   1077  1.146        ad 		}
   1078  1.109     darcy 		if (pt->pt_pfstype == PFSfile) {
   1079   1.75       chs 			fvp = p->p_textvp;
   1080   1.23   mycroft 			/* We already checked that it exists. */
   1081   1.32   mycroft 			VREF(fvp);
   1082   1.53      fvdl 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
   1083   1.32   mycroft 			*vpp = fvp;
   1084  1.146        ad 			procfs_proc_unlock(p);
   1085   1.21   mycroft 			return (0);
   1086   1.21   mycroft 		}
   1087    1.1        pk 
   1088   1.62  wrstuden 		error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
   1089  1.145        ad 		    pt->pt_pfstype, -1, p);
   1090  1.145        ad 		procfs_proc_unlock(p);
   1091   1.62  wrstuden 		return (error);
   1092    1.1        pk 
   1093  1.109     darcy 	case PFSfd: {
   1094   1.91  christos 		int fd;
   1095   1.91  christos 		struct file *fp;
   1096  1.145        ad 
   1097  1.145        ad 		if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ENOENT)) != 0)
   1098  1.145        ad 			return error;
   1099  1.145        ad 
   1100   1.91  christos 		/*
   1101   1.91  christos 		 * do the .. dance. We unlock the directory, and then
   1102   1.91  christos 		 * get the proc dir. That will automatically return ..
   1103  1.143       chs 		 * locked. Then re-lock the directory.
   1104   1.91  christos 		 */
   1105   1.91  christos 		if (cnp->cn_flags & ISDOTDOT) {
   1106   1.91  christos 			VOP_UNLOCK(dvp, 0);
   1107   1.91  christos 			error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
   1108  1.145        ad 			    PFSproc, -1, p);
   1109  1.146        ad 			vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
   1110  1.145        ad 			procfs_proc_unlock(p);
   1111   1.91  christos 			return (error);
   1112   1.91  christos 		}
   1113   1.91  christos 		fd = atoi(pname, cnp->cn_namelen);
   1114  1.145        ad 
   1115  1.145        ad 		mutex_enter(&p->p_mutex);
   1116  1.145        ad 		fp = fd_getfile(p->p_fd, fd);
   1117  1.145        ad 		mutex_exit(&p->p_mutex);
   1118  1.145        ad 		if (fp == NULL) {
   1119  1.145        ad 			procfs_proc_unlock(p);
   1120   1.91  christos 			return ENOENT;
   1121  1.145        ad 		}
   1122  1.145        ad 
   1123   1.99  jdolecek 		FILE_USE(fp);
   1124   1.99  jdolecek 
   1125   1.91  christos 		switch (fp->f_type) {
   1126   1.91  christos 		case DTYPE_VNODE:
   1127   1.91  christos 			fvp = (struct vnode *)fp->f_data;
   1128  1.101  jdolecek 
   1129  1.101  jdolecek 			/* Don't show directories */
   1130  1.102  christos 			if (fvp->v_type == VDIR)
   1131  1.102  christos 				goto symlink;
   1132  1.101  jdolecek 
   1133   1.91  christos 			VREF(fvp);
   1134  1.128  christos 			FILE_UNUSE(fp, l);
   1135  1.121     perry 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY |
   1136   1.92  christos 			    (p == curproc ? LK_CANRECURSE : 0));
   1137   1.91  christos 			*vpp = fvp;
   1138   1.91  christos 			error = 0;
   1139   1.91  christos 			break;
   1140   1.91  christos 		default:
   1141  1.102  christos 		symlink:
   1142  1.128  christos 			FILE_UNUSE(fp, l);
   1143   1.91  christos 			error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
   1144  1.145        ad 			    PFSfd, fd, p);
   1145   1.91  christos 			break;
   1146   1.91  christos 		}
   1147  1.145        ad 		procfs_proc_unlock(p);
   1148   1.91  christos 		return error;
   1149   1.91  christos 	}
   1150    1.9       cgd 	default:
   1151    1.9       cgd 		return (ENOTDIR);
   1152    1.1        pk 	}
   1153   1.32   mycroft 
   1154   1.32   mycroft 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
   1155    1.1        pk }
   1156    1.1        pk 
   1157   1.18       cgd int
   1158  1.138  christos procfs_validfile(struct lwp *l, struct mount *mp)
   1159   1.18       cgd {
   1160  1.136  christos 	return l != NULL && l->l_proc != NULL && l->l_proc->p_textvp != NULL;
   1161   1.74        tv }
   1162   1.21   mycroft 
   1163   1.74        tv static int
   1164  1.128  christos procfs_validfile_linux(l, mp)
   1165  1.128  christos 	struct lwp *l;
   1166   1.76      fvdl 	struct mount *mp;
   1167   1.74        tv {
   1168   1.76      fvdl 	int flags;
   1169   1.76      fvdl 
   1170   1.76      fvdl 	flags = VFSTOPROC(mp)->pmnt_flags;
   1171  1.142  christos 	return (flags & PROCFSMNT_LINUXCOMPAT) &&
   1172  1.142  christos 	    (l == NULL || l->l_proc == NULL || procfs_validfile(l, mp));
   1173   1.18       cgd }
   1174   1.18       cgd 
   1175  1.117      yamt struct procfs_root_readdir_ctx {
   1176  1.117      yamt 	struct uio *uiop;
   1177  1.117      yamt 	off_t *cookies;
   1178  1.117      yamt 	int ncookies;
   1179  1.120      yamt 	off_t off;
   1180  1.120      yamt 	off_t startoff;
   1181  1.117      yamt 	int error;
   1182  1.117      yamt };
   1183  1.117      yamt 
   1184  1.117      yamt static int
   1185  1.117      yamt procfs_root_readdir_callback(struct proc *p, void *arg)
   1186  1.117      yamt {
   1187  1.117      yamt 	struct procfs_root_readdir_ctx *ctxp = arg;
   1188  1.117      yamt 	struct dirent d;
   1189  1.117      yamt 	struct uio *uiop;
   1190  1.117      yamt 	int error;
   1191  1.117      yamt 
   1192  1.117      yamt 	uiop = ctxp->uiop;
   1193  1.117      yamt 	if (uiop->uio_resid < UIO_MX)
   1194  1.117      yamt 		return -1; /* no space */
   1195  1.117      yamt 
   1196  1.120      yamt 	if (ctxp->off < ctxp->startoff) {
   1197  1.120      yamt 		ctxp->off++;
   1198  1.117      yamt 		return 0;
   1199  1.117      yamt 	}
   1200  1.117      yamt 
   1201  1.133      yamt 	if (kauth_authorize_process(kauth_cred_get(),
   1202  1.133      yamt 	    KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0)
   1203  1.133      yamt 		return 0;
   1204  1.125      elad 
   1205  1.117      yamt 	memset(&d, 0, UIO_MX);
   1206  1.117      yamt 	d.d_reclen = UIO_MX;
   1207  1.117      yamt 	d.d_fileno = PROCFS_FILENO(p->p_pid, PFSproc, -1);
   1208  1.117      yamt 	d.d_namlen = snprintf(d.d_name,
   1209  1.117      yamt 	    UIO_MX - offsetof(struct dirent, d_name), "%ld", (long)p->p_pid);
   1210  1.117      yamt 	d.d_type = DT_DIR;
   1211  1.117      yamt 
   1212  1.145        ad 	rw_exit(&proclist_lock);
   1213  1.117      yamt 	error = uiomove(&d, UIO_MX, uiop);
   1214  1.145        ad 	rw_enter(&proclist_lock, RW_READER);
   1215  1.117      yamt 	if (error) {
   1216  1.117      yamt 		ctxp->error = error;
   1217  1.117      yamt 		return -1;
   1218  1.117      yamt 	}
   1219  1.117      yamt 
   1220  1.117      yamt 	ctxp->ncookies++;
   1221  1.117      yamt 	if (ctxp->cookies)
   1222  1.120      yamt 		*(ctxp->cookies)++ = ctxp->off + 1;
   1223  1.120      yamt 	ctxp->off++;
   1224  1.117      yamt 
   1225  1.117      yamt 	return 0;
   1226  1.117      yamt }
   1227  1.117      yamt 
   1228    1.9       cgd /*
   1229    1.9       cgd  * readdir returns directory entries from pfsnode (vp).
   1230    1.9       cgd  *
   1231    1.9       cgd  * the strategy here with procfs is to generate a single
   1232   1.34   mycroft  * directory entry at a time (struct dirent) and then
   1233    1.9       cgd  * copy that out to userland using uiomove.  a more efficent
   1234    1.9       cgd  * though more complex implementation, would try to minimize
   1235    1.9       cgd  * the number of calls to uiomove().  for procfs, this is
   1236    1.9       cgd  * hardly worth the added code complexity.
   1237    1.9       cgd  *
   1238    1.9       cgd  * this should just be done through read()
   1239    1.9       cgd  */
   1240   1.37  christos int
   1241   1.37  christos procfs_readdir(v)
   1242   1.37  christos 	void *v;
   1243   1.37  christos {
   1244   1.22   mycroft 	struct vop_readdir_args /* {
   1245   1.22   mycroft 		struct vnode *a_vp;
   1246   1.22   mycroft 		struct uio *a_uio;
   1247  1.130      elad 		kauth_cred_t a_cred;
   1248   1.22   mycroft 		int *a_eofflag;
   1249   1.53      fvdl 		off_t **a_cookies;
   1250   1.53      fvdl 		int *a_ncookies;
   1251   1.37  christos 	} */ *ap = v;
   1252   1.21   mycroft 	struct uio *uio = ap->a_uio;
   1253   1.34   mycroft 	struct dirent d;
   1254    1.9       cgd 	struct pfsnode *pfs;
   1255   1.68  sommerfe 	off_t i;
   1256    1.9       cgd 	int error;
   1257   1.53      fvdl 	off_t *cookies = NULL;
   1258  1.117      yamt 	int ncookies;
   1259   1.76      fvdl 	struct vnode *vp;
   1260   1.78  jdolecek 	const struct proc_target *pt;
   1261  1.117      yamt 	struct procfs_root_readdir_ctx ctx;
   1262  1.145        ad 	struct lwp *l;
   1263    1.9       cgd 
   1264   1.76      fvdl 	vp = ap->a_vp;
   1265   1.76      fvdl 	pfs = VTOPFS(vp);
   1266    1.1        pk 
   1267    1.9       cgd 	if (uio->uio_resid < UIO_MX)
   1268    1.9       cgd 		return (EINVAL);
   1269   1.35   mycroft 	if (uio->uio_offset < 0)
   1270    1.1        pk 		return (EINVAL);
   1271    1.1        pk 
   1272    1.9       cgd 	error = 0;
   1273   1.35   mycroft 	i = uio->uio_offset;
   1274  1.113       jrf 	memset(&d, 0, UIO_MX);
   1275   1.34   mycroft 	d.d_reclen = UIO_MX;
   1276   1.53      fvdl 	ncookies = uio->uio_resid / UIO_MX;
   1277    1.9       cgd 
   1278    1.9       cgd 	switch (pfs->pfs_type) {
   1279    1.9       cgd 	/*
   1280    1.9       cgd 	 * this is for the process-specific sub-directories.
   1281    1.9       cgd 	 * all that is needed to is copy out all the entries
   1282    1.9       cgd 	 * from the procent[] table (top of this file).
   1283    1.9       cgd 	 */
   1284  1.109     darcy 	case PFSproc: {
   1285   1.32   mycroft 		struct proc *p;
   1286   1.66  christos 
   1287   1.66  christos 		if (i >= nproc_targets)
   1288   1.67  sommerfe 			return 0;
   1289    1.9       cgd 
   1290  1.145        ad 		if (procfs_proc_lock(pfs->pfs_pid, &p, ESRCH) != 0)
   1291   1.32   mycroft 			break;
   1292   1.32   mycroft 
   1293   1.53      fvdl 		if (ap->a_ncookies) {
   1294   1.53      fvdl 			ncookies = min(ncookies, (nproc_targets - i));
   1295   1.72   thorpej 			cookies = malloc(ncookies * sizeof (off_t),
   1296   1.53      fvdl 			    M_TEMP, M_WAITOK);
   1297   1.53      fvdl 			*ap->a_cookies = cookies;
   1298   1.53      fvdl 		}
   1299   1.53      fvdl 
   1300   1.32   mycroft 		for (pt = &proc_targets[i];
   1301   1.32   mycroft 		     uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
   1302  1.145        ad 			if (pt->pt_valid) {
   1303  1.145        ad 				/* XXXSMP locking */
   1304  1.145        ad 				mutex_enter(&p->p_smutex);
   1305  1.145        ad 				l = proc_representative_lwp(p, NULL, 1);
   1306  1.145        ad 				mutex_exit(&p->p_smutex);
   1307  1.145        ad 				if ((*pt->pt_valid)(l, vp->v_mount) == 0)
   1308  1.145        ad 					continue;
   1309  1.145        ad 			}
   1310  1.121     perry 
   1311   1.91  christos 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
   1312   1.91  christos 			    pt->pt_pfstype, -1);
   1313   1.34   mycroft 			d.d_namlen = pt->pt_namlen;
   1314   1.56     perry 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
   1315   1.34   mycroft 			d.d_type = pt->pt_type;
   1316   1.15        ws 
   1317  1.113       jrf 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1318    1.9       cgd 				break;
   1319   1.53      fvdl 			if (cookies)
   1320   1.35   mycroft 				*cookies++ = i + 1;
   1321    1.6        ws 		}
   1322    1.9       cgd 
   1323  1.145        ad 		procfs_proc_unlock(p);
   1324    1.9       cgd 	    	break;
   1325   1.34   mycroft 	}
   1326  1.109     darcy 	case PFSfd: {
   1327   1.91  christos 		struct proc *p;
   1328   1.91  christos 		struct filedesc	*fdp;
   1329  1.111  jdolecek 		struct file *fp;
   1330  1.110    simonb 		int lim, nc = 0;
   1331   1.91  christos 
   1332  1.145        ad 		if ((error = procfs_proc_lock(pfs->pfs_pid, &p, ESRCH)) != 0)
   1333  1.145        ad 			return error;
   1334   1.91  christos 
   1335  1.133      yamt 		if (kauth_authorize_process(kauth_cred_get(),
   1336  1.145        ad 		    KAUTH_PROCESS_CANSEE, p, NULL, NULL, NULL) != 0) {
   1337  1.145        ad 		    	procfs_proc_unlock(p);
   1338  1.133      yamt 			return ESRCH;
   1339  1.145        ad 		}
   1340  1.125      elad 
   1341  1.145        ad 		fdp = p->p_fd;	/* XXXSMP */
   1342   1.91  christos 
   1343   1.91  christos 		lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
   1344  1.145        ad 		if (i >= lim) {
   1345  1.145        ad 		    	procfs_proc_unlock(p);
   1346   1.91  christos 			return 0;
   1347  1.145        ad 		}
   1348   1.91  christos 
   1349   1.91  christos 		if (ap->a_ncookies) {
   1350   1.91  christos 			ncookies = min(ncookies, (fdp->fd_nfiles + 2 - i));
   1351   1.91  christos 			cookies = malloc(ncookies * sizeof (off_t),
   1352   1.91  christos 			    M_TEMP, M_WAITOK);
   1353   1.91  christos 			*ap->a_cookies = cookies;
   1354   1.91  christos 		}
   1355   1.91  christos 
   1356   1.91  christos 		for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
   1357   1.91  christos 			pt = &proc_targets[i];
   1358   1.91  christos 			d.d_namlen = pt->pt_namlen;
   1359   1.91  christos 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid,
   1360   1.91  christos 			    pt->pt_pfstype, -1);
   1361   1.91  christos 			(void)memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
   1362   1.91  christos 			d.d_type = pt->pt_type;
   1363  1.113       jrf 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1364   1.91  christos 				break;
   1365   1.91  christos 			if (cookies)
   1366   1.91  christos 				*cookies++ = i + 1;
   1367   1.91  christos 			nc++;
   1368   1.91  christos 		}
   1369   1.91  christos 		if (error) {
   1370   1.91  christos 			ncookies = nc;
   1371   1.91  christos 			break;
   1372   1.91  christos 		}
   1373   1.91  christos 		for (; uio->uio_resid >= UIO_MX && i < fdp->fd_nfiles; i++) {
   1374  1.111  jdolecek 			/* check the descriptor exists */
   1375  1.111  jdolecek 			if ((fp = fd_getfile(fdp, i - 2)) == NULL)
   1376  1.100  jdolecek 				continue;
   1377  1.111  jdolecek 			simple_unlock(&fp->f_slock);
   1378  1.111  jdolecek 
   1379  1.109     darcy 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, PFSfd, i - 2);
   1380   1.91  christos 			d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
   1381   1.93    martin 			    "%lld", (long long)(i - 2));
   1382   1.91  christos 			d.d_type = VREG;
   1383  1.113       jrf 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1384   1.91  christos 				break;
   1385   1.91  christos 			if (cookies)
   1386   1.91  christos 				*cookies++ = i + 1;
   1387   1.91  christos 			nc++;
   1388   1.91  christos 		}
   1389   1.91  christos 		ncookies = nc;
   1390  1.145        ad 		procfs_proc_unlock(p);
   1391   1.91  christos 		break;
   1392   1.91  christos 	}
   1393    1.9       cgd 
   1394    1.9       cgd 	/*
   1395    1.9       cgd 	 * this is for the root of the procfs filesystem
   1396   1.69   thorpej 	 * what is needed are special entries for "curproc"
   1397   1.69   thorpej 	 * and "self" followed by an entry for each process
   1398  1.117      yamt 	 * on allproc.
   1399    1.9       cgd 	 */
   1400    1.9       cgd 
   1401  1.109     darcy 	case PFSroot: {
   1402  1.117      yamt 		int nc = 0;
   1403    1.9       cgd 
   1404   1.53      fvdl 		if (ap->a_ncookies) {
   1405   1.53      fvdl 			/*
   1406   1.53      fvdl 			 * XXX Potentially allocating too much space here,
   1407   1.53      fvdl 			 * but I'm lazy. This loop needs some work.
   1408   1.53      fvdl 			 */
   1409   1.72   thorpej 			cookies = malloc(ncookies * sizeof (off_t),
   1410   1.53      fvdl 			    M_TEMP, M_WAITOK);
   1411   1.53      fvdl 			*ap->a_cookies = cookies;
   1412   1.53      fvdl 		}
   1413  1.117      yamt 		error = 0;
   1414  1.117      yamt 		/* 0 ... 3 are static entries. */
   1415  1.117      yamt 		for (; i <= 3 && uio->uio_resid >= UIO_MX; i++) {
   1416    1.9       cgd 			switch (i) {
   1417   1.12        ws 			case 0:		/* `.' */
   1418   1.12        ws 			case 1:		/* `..' */
   1419  1.109     darcy 				d.d_fileno = PROCFS_FILENO(0, PFSroot, -1);
   1420   1.34   mycroft 				d.d_namlen = i + 1;
   1421   1.56     perry 				memcpy(d.d_name, "..", d.d_namlen);
   1422   1.34   mycroft 				d.d_name[i + 1] = '\0';
   1423   1.34   mycroft 				d.d_type = DT_DIR;
   1424   1.12        ws 				break;
   1425   1.21   mycroft 
   1426   1.12        ws 			case 2:
   1427  1.109     darcy 				d.d_fileno = PROCFS_FILENO(0, PFScurproc, -1);
   1428   1.69   thorpej 				d.d_namlen = sizeof("curproc") - 1;
   1429   1.69   thorpej 				memcpy(d.d_name, "curproc", sizeof("curproc"));
   1430   1.69   thorpej 				d.d_type = DT_LNK;
   1431   1.69   thorpej 				break;
   1432   1.69   thorpej 
   1433   1.69   thorpej 			case 3:
   1434  1.109     darcy 				d.d_fileno = PROCFS_FILENO(0, PFSself, -1);
   1435   1.69   thorpej 				d.d_namlen = sizeof("self") - 1;
   1436   1.69   thorpej 				memcpy(d.d_name, "self", sizeof("self"));
   1437   1.34   mycroft 				d.d_type = DT_LNK;
   1438    1.9       cgd 				break;
   1439    1.1        pk 			}
   1440   1.21   mycroft 
   1441  1.113       jrf 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1442    1.9       cgd 				break;
   1443   1.53      fvdl 			nc++;
   1444   1.53      fvdl 			if (cookies)
   1445   1.35   mycroft 				*cookies++ = i + 1;
   1446    1.1        pk 		}
   1447  1.117      yamt 		/* 4 ... are process entries. */
   1448  1.117      yamt 		ctx.uiop = uio;
   1449  1.117      yamt 		ctx.error = 0;
   1450  1.120      yamt 		ctx.off = 4;
   1451  1.120      yamt 		ctx.startoff = i;
   1452  1.117      yamt 		ctx.cookies = cookies;
   1453  1.117      yamt 		ctx.ncookies = nc;
   1454  1.117      yamt 		proclist_foreach_call(&allproc,
   1455  1.117      yamt 		    procfs_root_readdir_callback, &ctx);
   1456  1.117      yamt 		cookies = ctx.cookies;
   1457  1.117      yamt 		nc = ctx.ncookies;
   1458  1.117      yamt 		error = ctx.error;
   1459  1.117      yamt 		if (error)
   1460  1.117      yamt 			break;
   1461   1.76      fvdl 
   1462  1.117      yamt 		/* misc entries. */
   1463  1.120      yamt 		if (i < ctx.off)
   1464  1.120      yamt 			i = ctx.off;
   1465  1.120      yamt 		if (i >= ctx.off + nproc_root_targets)
   1466   1.76      fvdl 			break;
   1467  1.120      yamt 		for (pt = &proc_root_targets[i - ctx.off];
   1468  1.117      yamt 		    uio->uio_resid >= UIO_MX &&
   1469  1.117      yamt 		    pt < &proc_root_targets[nproc_root_targets];
   1470  1.117      yamt 		    pt++, i++) {
   1471   1.76      fvdl 			if (pt->pt_valid &&
   1472   1.76      fvdl 			    (*pt->pt_valid)(NULL, vp->v_mount) == 0)
   1473   1.76      fvdl 				continue;
   1474   1.91  christos 			d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype, -1);
   1475   1.76      fvdl 			d.d_namlen = pt->pt_namlen;
   1476   1.76      fvdl 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
   1477   1.76      fvdl 			d.d_type = pt->pt_type;
   1478   1.76      fvdl 
   1479  1.113       jrf 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1480   1.76      fvdl 				break;
   1481   1.76      fvdl 			nc++;
   1482   1.76      fvdl 			if (cookies)
   1483   1.76      fvdl 				*cookies++ = i + 1;
   1484   1.76      fvdl 		}
   1485   1.76      fvdl 
   1486   1.53      fvdl 		ncookies = nc;
   1487    1.9       cgd 		break;
   1488   1.34   mycroft 	}
   1489    1.9       cgd 
   1490    1.9       cgd 	default:
   1491    1.9       cgd 		error = ENOTDIR;
   1492    1.9       cgd 		break;
   1493    1.1        pk 	}
   1494    1.9       cgd 
   1495   1.53      fvdl 	if (ap->a_ncookies) {
   1496   1.53      fvdl 		if (error) {
   1497   1.54      fvdl 			if (cookies)
   1498   1.72   thorpej 				free(*ap->a_cookies, M_TEMP);
   1499   1.53      fvdl 			*ap->a_ncookies = 0;
   1500   1.53      fvdl 			*ap->a_cookies = NULL;
   1501   1.53      fvdl 		} else
   1502   1.53      fvdl 			*ap->a_ncookies = ncookies;
   1503   1.53      fvdl 	}
   1504   1.35   mycroft 	uio->uio_offset = i;
   1505    1.9       cgd 	return (error);
   1506   1.12        ws }
   1507   1.12        ws 
   1508   1.12        ws /*
   1509  1.126    atatat  * readlink reads the link of `curproc' and others
   1510   1.12        ws  */
   1511   1.37  christos int
   1512   1.37  christos procfs_readlink(v)
   1513   1.37  christos 	void *v;
   1514   1.12        ws {
   1515   1.37  christos 	struct vop_readlink_args *ap = v;
   1516  1.123  christos 	char bf[16];		/* should be enough */
   1517  1.123  christos 	char *bp = bf;
   1518  1.102  christos 	char *path = NULL;
   1519   1.12        ws 	int len;
   1520  1.102  christos 	int error = 0;
   1521  1.102  christos 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
   1522  1.126    atatat 	struct proc *pown;
   1523   1.12        ws 
   1524  1.109     darcy 	if (pfs->pfs_fileno == PROCFS_FILENO(0, PFScurproc, -1))
   1525  1.123  christos 		len = snprintf(bf, sizeof(bf), "%ld", (long)curproc->p_pid);
   1526  1.109     darcy 	else if (pfs->pfs_fileno == PROCFS_FILENO(0, PFSself, -1))
   1527  1.123  christos 		len = snprintf(bf, sizeof(bf), "%s", "curproc");
   1528  1.136  christos 	else if (pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFScwd, -1) ||
   1529  1.136  christos 	    pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSchroot, -1) ||
   1530  1.136  christos 	    pfs->pfs_fileno == PROCFS_FILENO(pfs->pfs_pid, PFSexe, -1)) {
   1531  1.145        ad 		if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
   1532  1.145        ad 			return error;
   1533  1.126    atatat 		MALLOC(path, char *, MAXPATHLEN + 4, M_TEMP,
   1534  1.126    atatat 		    M_WAITOK|M_CANFAIL);
   1535  1.145        ad 		if (path == NULL) {
   1536  1.145        ad 			procfs_proc_unlock(pown);
   1537  1.126    atatat 			return (ENOMEM);
   1538  1.145        ad 		}
   1539  1.126    atatat 		bp = path + MAXPATHLEN;
   1540  1.126    atatat 		*--bp = '\0';
   1541  1.145        ad 		mutex_enter(&pown->p_mutex);
   1542  1.136  christos 		(void)procfs_dir(PROCFS_TYPE(pfs->pfs_fileno), curlwp, pown,
   1543  1.136  christos 		    &bp, path, MAXPATHLEN);
   1544  1.145        ad 		mutex_exit(&pown->p_mutex);
   1545  1.145        ad 		procfs_proc_unlock(pown);
   1546  1.126    atatat 		len = strlen(bp);
   1547  1.136  christos 	} else {
   1548  1.102  christos 		struct file *fp;
   1549  1.102  christos 		struct vnode *vxp, *vp;
   1550  1.102  christos 
   1551  1.145        ad 		if ((error = procfs_proc_lock(pfs->pfs_pid, &pown, ESRCH)) != 0)
   1552  1.102  christos 			return error;
   1553  1.145        ad 		mutex_enter(&pown->p_mutex);
   1554  1.145        ad 		fp = fd_getfile(pown->p_fd, pfs->pfs_fd);
   1555  1.145        ad 		mutex_exit(&pown->p_mutex);
   1556  1.145        ad 		if (error != 0) {
   1557  1.145        ad 			procfs_proc_unlock(pown);
   1558  1.145        ad 			return (EBADF);
   1559  1.145        ad 		}
   1560  1.102  christos 		FILE_USE(fp);
   1561  1.102  christos 		switch (fp->f_type) {
   1562  1.102  christos 		case DTYPE_VNODE:
   1563  1.102  christos 			vxp = (struct vnode *)fp->f_data;
   1564  1.102  christos 			if (vxp->v_type != VDIR) {
   1565  1.145        ad 				FILE_UNUSE(fp, curlwp);
   1566  1.145        ad 				error = EINVAL;
   1567  1.145        ad 				break;
   1568  1.102  christos 			}
   1569  1.102  christos 			if ((path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK))
   1570  1.102  christos 			    == NULL) {
   1571  1.145        ad 				FILE_UNUSE(fp, curlwp);
   1572  1.145        ad 				error = ENOMEM;
   1573  1.145        ad 				break;
   1574  1.102  christos 			}
   1575  1.102  christos 			bp = path + MAXPATHLEN;
   1576  1.102  christos 			*--bp = '\0';
   1577  1.145        ad 			vp = curproc->p_cwdi->cwdi_rdir;	/* XXXSMP */
   1578  1.102  christos 			if (vp == NULL)
   1579  1.102  christos 				vp = rootvnode;
   1580  1.102  christos 			error = getcwd_common(vxp, vp, &bp, path,
   1581  1.128  christos 			    MAXPATHLEN / 2, 0, curlwp);
   1582  1.145        ad 			FILE_UNUSE(fp, curlwp);
   1583  1.145        ad 			if (error)
   1584  1.145        ad 				break;
   1585  1.102  christos 			len = strlen(bp);
   1586  1.102  christos 			break;
   1587  1.102  christos 
   1588  1.102  christos 		case DTYPE_MISC:
   1589  1.123  christos 			len = snprintf(bf, sizeof(bf), "%s", "[misc]");
   1590  1.102  christos 			break;
   1591  1.102  christos 
   1592  1.102  christos 		case DTYPE_KQUEUE:
   1593  1.123  christos 			len = snprintf(bf, sizeof(bf), "%s", "[kqueue]");
   1594  1.102  christos 			break;
   1595  1.102  christos 
   1596  1.102  christos 		default:
   1597  1.145        ad 			error = EINVAL;
   1598  1.145        ad 			break;
   1599  1.145        ad 		}
   1600  1.145        ad 		procfs_proc_unlock(pown);
   1601  1.102  christos 	}
   1602   1.12        ws 
   1603  1.145        ad 	if (error == 0)
   1604  1.145        ad 		error = uiomove(bp, len, ap->a_uio);
   1605  1.102  christos 	if (path)
   1606  1.102  christos 		free(path, M_TEMP);
   1607  1.102  christos 	return error;
   1608    1.1        pk }
   1609    1.1        pk 
   1610    1.1        pk /*
   1611   1.91  christos  * convert decimal ascii to int
   1612    1.1        pk  */
   1613   1.91  christos static int
   1614   1.91  christos atoi(b, len)
   1615    1.9       cgd 	const char *b;
   1616   1.91  christos 	size_t len;
   1617    1.1        pk {
   1618   1.91  christos 	int p = 0;
   1619    1.1        pk 
   1620    1.1        pk 	while (len--) {
   1621    1.9       cgd 		char c = *b++;
   1622    1.1        pk 		if (c < '0' || c > '9')
   1623   1.91  christos 			return -1;
   1624    1.9       cgd 		p = 10 * p + (c - '0');
   1625    1.1        pk 	}
   1626    1.9       cgd 
   1627   1.91  christos 	return p;
   1628    1.1        pk }
   1629