Home | History | Annotate | Line # | Download | only in procfs
procfs_vnops.c revision 1.1.1.2
      1      1.1    pk /*
      2  1.1.1.2  fvdl  * Copyright (c) 1993, 1995 Jan-Simon Pendry
      3  1.1.1.2  fvdl  * Copyright (c) 1993, 1995
      4  1.1.1.1  fvdl  *	The Regents of the University of California.  All rights reserved.
      5  1.1.1.1  fvdl  *
      6  1.1.1.1  fvdl  * This code is derived from software contributed to Berkeley by
      7  1.1.1.1  fvdl  * Jan-Simon Pendry.
      8  1.1.1.1  fvdl  *
      9  1.1.1.1  fvdl  * Redistribution and use in source and binary forms, with or without
     10  1.1.1.1  fvdl  * modification, are permitted provided that the following conditions
     11  1.1.1.1  fvdl  * are met:
     12  1.1.1.1  fvdl  * 1. Redistributions of source code must retain the above copyright
     13  1.1.1.1  fvdl  *    notice, this list of conditions and the following disclaimer.
     14  1.1.1.1  fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1.1.1  fvdl  *    notice, this list of conditions and the following disclaimer in the
     16  1.1.1.1  fvdl  *    documentation and/or other materials provided with the distribution.
     17  1.1.1.1  fvdl  * 3. All advertising materials mentioning features or use of this software
     18  1.1.1.1  fvdl  *    must display the following acknowledgement:
     19  1.1.1.1  fvdl  *	This product includes software developed by the University of
     20  1.1.1.1  fvdl  *	California, Berkeley and its contributors.
     21  1.1.1.1  fvdl  * 4. Neither the name of the University nor the names of its contributors
     22  1.1.1.1  fvdl  *    may be used to endorse or promote products derived from this software
     23  1.1.1.1  fvdl  *    without specific prior written permission.
     24  1.1.1.1  fvdl  *
     25  1.1.1.1  fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1.1.1  fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1.1.1  fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1.1.1  fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1.1.1  fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1.1.1  fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1.1.1  fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1.1.1  fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1.1.1  fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1.1.1  fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1.1.1  fvdl  * SUCH DAMAGE.
     36  1.1.1.1  fvdl  *
     37  1.1.1.2  fvdl  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
     38  1.1.1.1  fvdl  *
     39  1.1.1.1  fvdl  * From:
     40  1.1.1.1  fvdl  *	$Id: procfs_vnops.c,v 1.1.1.2 1998/03/01 02:13:19 fvdl Exp $
     41      1.1    pk  */
     42      1.1    pk 
     43      1.1    pk /*
     44  1.1.1.1  fvdl  * procfs vnode interface
     45      1.1    pk  */
     46  1.1.1.1  fvdl 
     47  1.1.1.1  fvdl #include <sys/param.h>
     48  1.1.1.1  fvdl #include <sys/systm.h>
     49  1.1.1.1  fvdl #include <sys/time.h>
     50  1.1.1.1  fvdl #include <sys/kernel.h>
     51  1.1.1.1  fvdl #include <sys/file.h>
     52  1.1.1.1  fvdl #include <sys/proc.h>
     53  1.1.1.1  fvdl #include <sys/vnode.h>
     54  1.1.1.1  fvdl #include <sys/namei.h>
     55  1.1.1.1  fvdl #include <sys/malloc.h>
     56  1.1.1.1  fvdl #include <sys/dirent.h>
     57  1.1.1.1  fvdl #include <sys/resourcevar.h>
     58  1.1.1.1  fvdl #include <vm/vm.h>	/* for PAGE_SIZE */
     59  1.1.1.2  fvdl #include <machine/reg.h>
     60  1.1.1.2  fvdl #include <miscfs/procfs/procfs.h>
     61      1.1    pk 
     62      1.1    pk /*
     63      1.1    pk  * Vnode Operations.
     64      1.1    pk  *
     65      1.1    pk  */
     66      1.1    pk 
     67  1.1.1.1  fvdl /*
     68  1.1.1.1  fvdl  * This is a list of the valid names in the
     69  1.1.1.1  fvdl  * process-specific sub-directories.  It is
     70  1.1.1.1  fvdl  * used in procfs_lookup and procfs_readdir
     71  1.1.1.1  fvdl  */
     72  1.1.1.2  fvdl struct proc_target {
     73  1.1.1.2  fvdl 	u_char	pt_type;
     74  1.1.1.2  fvdl 	u_char	pt_namlen;
     75  1.1.1.2  fvdl 	char	*pt_name;
     76  1.1.1.2  fvdl 	pfstype	pt_pfstype;
     77  1.1.1.2  fvdl 	int	(*pt_valid) __P((struct proc *p));
     78  1.1.1.2  fvdl } proc_targets[] = {
     79  1.1.1.1  fvdl #define N(s) sizeof(s)-1, s
     80  1.1.1.2  fvdl 	/*	  name		type		validp */
     81  1.1.1.2  fvdl 	{ DT_DIR, N("."),	Pproc,		NULL },
     82  1.1.1.2  fvdl 	{ DT_DIR, N(".."),	Proot,		NULL },
     83  1.1.1.2  fvdl 	{ DT_REG, N("file"),	Pfile,		procfs_validfile },
     84  1.1.1.2  fvdl 	{ DT_REG, N("mem"),	Pmem,		NULL },
     85  1.1.1.2  fvdl 	{ DT_REG, N("regs"),	Pregs,		procfs_validregs },
     86  1.1.1.2  fvdl 	{ DT_REG, N("fpregs"),	Pfpregs,	procfs_validfpregs },
     87  1.1.1.2  fvdl 	{ DT_REG, N("ctl"),	Pctl,		NULL },
     88  1.1.1.2  fvdl 	{ DT_REG, N("status"),	Pstatus,	NULL },
     89  1.1.1.2  fvdl 	{ DT_REG, N("note"),	Pnote,		NULL },
     90  1.1.1.2  fvdl 	{ DT_REG, N("notepg"),	Pnotepg,	NULL },
     91  1.1.1.1  fvdl #undef N
     92  1.1.1.1  fvdl };
     93  1.1.1.2  fvdl static int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
     94      1.1    pk 
     95  1.1.1.1  fvdl static pid_t atopid __P((const char *, u_int));
     96      1.1    pk 
     97      1.1    pk /*
     98  1.1.1.1  fvdl  * set things up for doing i/o on
     99  1.1.1.1  fvdl  * the pfsnode (vp).  (vp) is locked
    100  1.1.1.1  fvdl  * on entry, and should be left locked
    101  1.1.1.1  fvdl  * on exit.
    102  1.1.1.1  fvdl  *
    103  1.1.1.1  fvdl  * for procfs we don't need to do anything
    104  1.1.1.1  fvdl  * in particular for i/o.  all that is done
    105  1.1.1.1  fvdl  * is to support exclusive open on process
    106  1.1.1.1  fvdl  * memory images.
    107      1.1    pk  */
    108  1.1.1.1  fvdl procfs_open(ap)
    109  1.1.1.2  fvdl 	struct vop_open_args /* {
    110  1.1.1.2  fvdl 		struct vnode *a_vp;
    111  1.1.1.2  fvdl 		int  a_mode;
    112  1.1.1.2  fvdl 		struct ucred *a_cred;
    113  1.1.1.2  fvdl 		struct proc *a_p;
    114  1.1.1.2  fvdl 	} */ *ap;
    115      1.1    pk {
    116  1.1.1.1  fvdl 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    117      1.1    pk 
    118  1.1.1.1  fvdl 	switch (pfs->pfs_type) {
    119  1.1.1.1  fvdl 	case Pmem:
    120  1.1.1.1  fvdl 		if (PFIND(pfs->pfs_pid) == 0)
    121  1.1.1.1  fvdl 			return (ENOENT);	/* was ESRCH, jsp */
    122      1.1    pk 
    123  1.1.1.1  fvdl 		if ((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL) ||
    124  1.1.1.2  fvdl 		    (pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))
    125  1.1.1.1  fvdl 			return (EBUSY);
    126      1.1    pk 
    127  1.1.1.1  fvdl 		if (ap->a_mode & FWRITE)
    128  1.1.1.1  fvdl 			pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
    129      1.1    pk 
    130  1.1.1.1  fvdl 		return (0);
    131      1.1    pk 
    132  1.1.1.1  fvdl 	default:
    133      1.1    pk 		break;
    134  1.1.1.1  fvdl 	}
    135      1.1    pk 
    136  1.1.1.1  fvdl 	return (0);
    137  1.1.1.1  fvdl }
    138      1.1    pk 
    139  1.1.1.1  fvdl /*
    140  1.1.1.1  fvdl  * close the pfsnode (vp) after doing i/o.
    141  1.1.1.1  fvdl  * (vp) is not locked on entry or exit.
    142  1.1.1.1  fvdl  *
    143  1.1.1.1  fvdl  * nothing to do for procfs other than undo
    144  1.1.1.1  fvdl  * any exclusive open flag (see _open above).
    145  1.1.1.1  fvdl  */
    146  1.1.1.1  fvdl procfs_close(ap)
    147  1.1.1.2  fvdl 	struct vop_close_args /* {
    148  1.1.1.2  fvdl 		struct vnode *a_vp;
    149  1.1.1.2  fvdl 		int  a_fflag;
    150  1.1.1.2  fvdl 		struct ucred *a_cred;
    151  1.1.1.2  fvdl 		struct proc *a_p;
    152  1.1.1.2  fvdl 	} */ *ap;
    153  1.1.1.1  fvdl {
    154  1.1.1.1  fvdl 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    155      1.1    pk 
    156  1.1.1.1  fvdl 	switch (pfs->pfs_type) {
    157  1.1.1.1  fvdl 	case Pmem:
    158  1.1.1.1  fvdl 		if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
    159  1.1.1.1  fvdl 			pfs->pfs_flags &= ~(FWRITE|O_EXCL);
    160      1.1    pk 		break;
    161  1.1.1.1  fvdl 	}
    162      1.1    pk 
    163  1.1.1.1  fvdl 	return (0);
    164  1.1.1.1  fvdl }
    165      1.1    pk 
    166  1.1.1.1  fvdl /*
    167  1.1.1.1  fvdl  * do an ioctl operation on pfsnode (vp).
    168  1.1.1.1  fvdl  * (vp) is not locked on entry or exit.
    169  1.1.1.1  fvdl  */
    170  1.1.1.1  fvdl procfs_ioctl(ap)
    171  1.1.1.2  fvdl 	struct vop_ioctl_args /* {
    172  1.1.1.2  fvdl 		struct vnode *a_vp;
    173  1.1.1.2  fvdl 		int a_command;
    174  1.1.1.2  fvdl 		caddr_t a_data;
    175  1.1.1.2  fvdl 		int a_fflag;
    176  1.1.1.2  fvdl 		struct ucred *a_cred;
    177  1.1.1.2  fvdl 		struct proc *a_p;
    178  1.1.1.2  fvdl 	} */ *ap;
    179  1.1.1.1  fvdl {
    180  1.1.1.1  fvdl 
    181  1.1.1.1  fvdl 	return (ENOTTY);
    182      1.1    pk }
    183      1.1    pk 
    184      1.1    pk /*
    185  1.1.1.1  fvdl  * do block mapping for pfsnode (vp).
    186  1.1.1.1  fvdl  * since we don't use the buffer cache
    187  1.1.1.1  fvdl  * for procfs this function should never
    188  1.1.1.1  fvdl  * be called.  in any case, it's not clear
    189  1.1.1.1  fvdl  * what part of the kernel ever makes use
    190  1.1.1.1  fvdl  * of this function.  for sanity, this is the
    191  1.1.1.1  fvdl  * usual no-op bmap, although returning
    192  1.1.1.1  fvdl  * (EIO) would be a reasonable alternative.
    193      1.1    pk  */
    194  1.1.1.1  fvdl procfs_bmap(ap)
    195  1.1.1.2  fvdl 	struct vop_bmap_args /* {
    196  1.1.1.2  fvdl 		struct vnode *a_vp;
    197  1.1.1.2  fvdl 		daddr_t  a_bn;
    198  1.1.1.2  fvdl 		struct vnode **a_vpp;
    199  1.1.1.2  fvdl 		daddr_t *a_bnp;
    200  1.1.1.2  fvdl 		int *a_runp;
    201  1.1.1.2  fvdl 	} */ *ap;
    202      1.1    pk {
    203      1.1    pk 
    204  1.1.1.1  fvdl 	if (ap->a_vpp != NULL)
    205  1.1.1.1  fvdl 		*ap->a_vpp = ap->a_vp;
    206  1.1.1.1  fvdl 	if (ap->a_bnp != NULL)
    207  1.1.1.1  fvdl 		*ap->a_bnp = ap->a_bn;
    208  1.1.1.2  fvdl 	if (ap->a_runp != NULL)
    209  1.1.1.2  fvdl 		*ap->a_runp = 0;
    210      1.1    pk 	return (0);
    211      1.1    pk }
    212      1.1    pk 
    213      1.1    pk /*
    214  1.1.1.2  fvdl  * procfs_inactive is called when the pfsnode
    215  1.1.1.1  fvdl  * is vrele'd and the reference count goes
    216  1.1.1.1  fvdl  * to zero.  (vp) will be on the vnode free
    217  1.1.1.1  fvdl  * list, so to get it back vget() must be
    218  1.1.1.1  fvdl  * used.
    219  1.1.1.1  fvdl  *
    220  1.1.1.1  fvdl  * for procfs, check if the process is still
    221  1.1.1.1  fvdl  * alive and if it isn't then just throw away
    222  1.1.1.1  fvdl  * the vnode by calling vgone().  this may
    223  1.1.1.1  fvdl  * be overkill and a waste of time since the
    224  1.1.1.1  fvdl  * chances are that the process will still be
    225  1.1.1.1  fvdl  * there and PFIND is not free.
    226  1.1.1.1  fvdl  *
    227  1.1.1.2  fvdl  * (vp) is locked on entry, but must be unlocked on exit.
    228      1.1    pk  */
    229  1.1.1.1  fvdl procfs_inactive(ap)
    230  1.1.1.2  fvdl 	struct vop_inactive_args /* {
    231  1.1.1.2  fvdl 		struct vnode *a_vp;
    232  1.1.1.2  fvdl 	} */ *ap;
    233      1.1    pk {
    234  1.1.1.2  fvdl 	struct vnode *vp = ap->a_vp;
    235  1.1.1.2  fvdl 	struct pfsnode *pfs = VTOPFS(vp);
    236  1.1.1.1  fvdl 
    237  1.1.1.2  fvdl 	VOP_UNLOCK(vp, 0, ap->a_p);
    238  1.1.1.1  fvdl 	if (PFIND(pfs->pfs_pid) == 0)
    239  1.1.1.2  fvdl 		vgone(vp);
    240      1.1    pk 
    241      1.1    pk 	return (0);
    242      1.1    pk }
    243      1.1    pk 
    244      1.1    pk /*
    245  1.1.1.1  fvdl  * _reclaim is called when getnewvnode()
    246  1.1.1.1  fvdl  * wants to make use of an entry on the vnode
    247  1.1.1.1  fvdl  * free list.  at this time the filesystem needs
    248  1.1.1.1  fvdl  * to free any private data and remove the node
    249  1.1.1.1  fvdl  * from any private lists.
    250      1.1    pk  */
    251  1.1.1.1  fvdl procfs_reclaim(ap)
    252  1.1.1.2  fvdl 	struct vop_reclaim_args /* {
    253  1.1.1.2  fvdl 		struct vnode *a_vp;
    254  1.1.1.2  fvdl 	} */ *ap;
    255  1.1.1.1  fvdl {
    256  1.1.1.1  fvdl 
    257  1.1.1.2  fvdl 	return (procfs_freevp(ap->a_vp));
    258      1.1    pk }
    259      1.1    pk 
    260      1.1    pk /*
    261  1.1.1.1  fvdl  * Return POSIX pathconf information applicable to special devices.
    262      1.1    pk  */
    263  1.1.1.1  fvdl procfs_pathconf(ap)
    264  1.1.1.1  fvdl 	struct vop_pathconf_args /* {
    265  1.1.1.1  fvdl 		struct vnode *a_vp;
    266  1.1.1.1  fvdl 		int a_name;
    267  1.1.1.1  fvdl 		int *a_retval;
    268  1.1.1.1  fvdl 	} */ *ap;
    269      1.1    pk {
    270      1.1    pk 
    271  1.1.1.1  fvdl 	switch (ap->a_name) {
    272  1.1.1.1  fvdl 	case _PC_LINK_MAX:
    273  1.1.1.1  fvdl 		*ap->a_retval = LINK_MAX;
    274  1.1.1.1  fvdl 		return (0);
    275  1.1.1.1  fvdl 	case _PC_MAX_CANON:
    276  1.1.1.1  fvdl 		*ap->a_retval = MAX_CANON;
    277  1.1.1.1  fvdl 		return (0);
    278  1.1.1.1  fvdl 	case _PC_MAX_INPUT:
    279  1.1.1.1  fvdl 		*ap->a_retval = MAX_INPUT;
    280  1.1.1.1  fvdl 		return (0);
    281  1.1.1.1  fvdl 	case _PC_PIPE_BUF:
    282  1.1.1.1  fvdl 		*ap->a_retval = PIPE_BUF;
    283  1.1.1.1  fvdl 		return (0);
    284  1.1.1.1  fvdl 	case _PC_CHOWN_RESTRICTED:
    285  1.1.1.1  fvdl 		*ap->a_retval = 1;
    286  1.1.1.1  fvdl 		return (0);
    287  1.1.1.1  fvdl 	case _PC_VDISABLE:
    288  1.1.1.1  fvdl 		*ap->a_retval = _POSIX_VDISABLE;
    289  1.1.1.1  fvdl 		return (0);
    290  1.1.1.1  fvdl 	default:
    291  1.1.1.1  fvdl 		return (EINVAL);
    292      1.1    pk 	}
    293  1.1.1.1  fvdl 	/* NOTREACHED */
    294      1.1    pk }
    295      1.1    pk 
    296      1.1    pk /*
    297  1.1.1.1  fvdl  * _print is used for debugging.
    298  1.1.1.1  fvdl  * just print a readable description
    299  1.1.1.1  fvdl  * of (vp).
    300      1.1    pk  */
    301  1.1.1.1  fvdl procfs_print(ap)
    302  1.1.1.2  fvdl 	struct vop_print_args /* {
    303  1.1.1.2  fvdl 		struct vnode *a_vp;
    304  1.1.1.2  fvdl 	} */ *ap;
    305      1.1    pk {
    306  1.1.1.1  fvdl 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    307  1.1.1.1  fvdl 
    308  1.1.1.2  fvdl 	printf("tag VT_PROCFS, type %s, pid %d, mode %x, flags %x\n",
    309  1.1.1.2  fvdl 	    pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
    310      1.1    pk }
    311      1.1    pk 
    312      1.1    pk /*
    313  1.1.1.1  fvdl  * _abortop is called when operations such as
    314  1.1.1.1  fvdl  * rename and create fail.  this entry is responsible
    315  1.1.1.1  fvdl  * for undoing any side-effects caused by the lookup.
    316  1.1.1.1  fvdl  * this will always include freeing the pathname buffer.
    317      1.1    pk  */
    318  1.1.1.1  fvdl procfs_abortop(ap)
    319  1.1.1.2  fvdl 	struct vop_abortop_args /* {
    320  1.1.1.2  fvdl 		struct vnode *a_dvp;
    321  1.1.1.2  fvdl 		struct componentname *a_cnp;
    322  1.1.1.2  fvdl 	} */ *ap;
    323      1.1    pk {
    324  1.1.1.1  fvdl 
    325  1.1.1.1  fvdl 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    326  1.1.1.1  fvdl 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
    327  1.1.1.1  fvdl 	return (0);
    328      1.1    pk }
    329      1.1    pk 
    330      1.1    pk /*
    331  1.1.1.1  fvdl  * generic entry point for unsupported operations
    332      1.1    pk  */
    333  1.1.1.1  fvdl procfs_badop()
    334      1.1    pk {
    335      1.1    pk 
    336  1.1.1.1  fvdl 	return (EIO);
    337  1.1.1.1  fvdl }
    338      1.1    pk 
    339  1.1.1.1  fvdl /*
    340  1.1.1.1  fvdl  * Invent attributes for pfsnode (vp) and store
    341  1.1.1.1  fvdl  * them in (vap).
    342  1.1.1.1  fvdl  * Directories lengths are returned as zero since
    343  1.1.1.1  fvdl  * any real length would require the genuine size
    344  1.1.1.1  fvdl  * to be computed, and nothing cares anyway.
    345  1.1.1.1  fvdl  *
    346  1.1.1.1  fvdl  * this is relatively minimal for procfs.
    347  1.1.1.1  fvdl  */
    348  1.1.1.1  fvdl procfs_getattr(ap)
    349  1.1.1.2  fvdl 	struct vop_getattr_args /* {
    350  1.1.1.2  fvdl 		struct vnode *a_vp;
    351  1.1.1.2  fvdl 		struct vattr *a_vap;
    352  1.1.1.2  fvdl 		struct ucred *a_cred;
    353  1.1.1.2  fvdl 		struct proc *a_p;
    354  1.1.1.2  fvdl 	} */ *ap;
    355  1.1.1.1  fvdl {
    356  1.1.1.1  fvdl 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    357  1.1.1.1  fvdl 	struct vattr *vap = ap->a_vap;
    358  1.1.1.1  fvdl 	struct proc *procp;
    359  1.1.1.2  fvdl 	struct timeval tv;
    360  1.1.1.1  fvdl 	int error;
    361      1.1    pk 
    362  1.1.1.1  fvdl 	/* first check the process still exists */
    363  1.1.1.1  fvdl 	switch (pfs->pfs_type) {
    364  1.1.1.1  fvdl 	case Proot:
    365  1.1.1.2  fvdl 	case Pcurproc:
    366  1.1.1.1  fvdl 		procp = 0;
    367  1.1.1.1  fvdl 		break;
    368      1.1    pk 
    369  1.1.1.1  fvdl 	default:
    370  1.1.1.1  fvdl 		procp = PFIND(pfs->pfs_pid);
    371  1.1.1.1  fvdl 		if (procp == 0)
    372  1.1.1.1  fvdl 			return (ENOENT);
    373  1.1.1.1  fvdl 	}
    374      1.1    pk 
    375  1.1.1.1  fvdl 	error = 0;
    376      1.1    pk 
    377  1.1.1.1  fvdl 	/* start by zeroing out the attributes */
    378  1.1.1.1  fvdl 	VATTR_NULL(vap);
    379      1.1    pk 
    380  1.1.1.1  fvdl 	/* next do all the common fields */
    381  1.1.1.1  fvdl 	vap->va_type = ap->a_vp->v_type;
    382  1.1.1.1  fvdl 	vap->va_mode = pfs->pfs_mode;
    383  1.1.1.1  fvdl 	vap->va_fileid = pfs->pfs_fileno;
    384  1.1.1.1  fvdl 	vap->va_flags = 0;
    385  1.1.1.1  fvdl 	vap->va_blocksize = PAGE_SIZE;
    386  1.1.1.1  fvdl 	vap->va_bytes = vap->va_size = 0;
    387      1.1    pk 
    388  1.1.1.1  fvdl 	/*
    389  1.1.1.2  fvdl 	 * Make all times be current TOD.
    390  1.1.1.2  fvdl 	 * It would be possible to get the process start
    391  1.1.1.2  fvdl 	 * time from the p_stat structure, but there's
    392  1.1.1.2  fvdl 	 * no "file creation" time stamp anyway, and the
    393  1.1.1.2  fvdl 	 * p_stat structure is not addressible if u. gets
    394  1.1.1.2  fvdl 	 * swapped out for that process.
    395  1.1.1.2  fvdl 	 */
    396  1.1.1.2  fvdl 	microtime(&tv);
    397  1.1.1.2  fvdl 	TIMEVAL_TO_TIMESPEC(&tv, &vap->va_ctime);
    398  1.1.1.2  fvdl 	vap->va_atime = vap->va_mtime = vap->va_ctime;
    399  1.1.1.2  fvdl 
    400  1.1.1.2  fvdl 	/*
    401  1.1.1.1  fvdl 	 * If the process has exercised some setuid or setgid
    402  1.1.1.1  fvdl 	 * privilege, then rip away read/write permission so
    403  1.1.1.1  fvdl 	 * that only root can gain access.
    404  1.1.1.1  fvdl 	 */
    405  1.1.1.1  fvdl 	switch (pfs->pfs_type) {
    406  1.1.1.2  fvdl 	case Pmem:
    407  1.1.1.1  fvdl 	case Pregs:
    408  1.1.1.1  fvdl 	case Pfpregs:
    409  1.1.1.1  fvdl 		if (procp->p_flag & P_SUGID)
    410  1.1.1.1  fvdl 			vap->va_mode &= ~((VREAD|VWRITE)|
    411  1.1.1.1  fvdl 					  ((VREAD|VWRITE)>>3)|
    412  1.1.1.1  fvdl 					  ((VREAD|VWRITE)>>6));
    413  1.1.1.2  fvdl 	case Pctl:
    414  1.1.1.2  fvdl 	case Pstatus:
    415  1.1.1.2  fvdl 	case Pnote:
    416  1.1.1.2  fvdl 	case Pnotepg:
    417  1.1.1.2  fvdl 		vap->va_nlink = 1;
    418  1.1.1.2  fvdl 		vap->va_uid = procp->p_ucred->cr_uid;
    419  1.1.1.2  fvdl 		vap->va_gid = procp->p_ucred->cr_gid;
    420  1.1.1.1  fvdl 		break;
    421  1.1.1.1  fvdl 	}
    422      1.1    pk 
    423  1.1.1.1  fvdl 	/*
    424  1.1.1.1  fvdl 	 * now do the object specific fields
    425  1.1.1.1  fvdl 	 *
    426  1.1.1.1  fvdl 	 * The size could be set from struct reg, but it's hardly
    427  1.1.1.1  fvdl 	 * worth the trouble, and it puts some (potentially) machine
    428  1.1.1.1  fvdl 	 * dependent data into this machine-independent code.  If it
    429  1.1.1.1  fvdl 	 * becomes important then this function should break out into
    430  1.1.1.1  fvdl 	 * a per-file stat function in the corresponding .c file.
    431  1.1.1.1  fvdl 	 */
    432      1.1    pk 
    433  1.1.1.1  fvdl 	switch (pfs->pfs_type) {
    434  1.1.1.1  fvdl 	case Proot:
    435  1.1.1.2  fvdl 		/*
    436  1.1.1.2  fvdl 		 * Set nlink to 1 to tell fts(3) we don't actually know.
    437  1.1.1.2  fvdl 		 */
    438  1.1.1.2  fvdl 		vap->va_nlink = 1;
    439      1.1    pk 		vap->va_uid = 0;
    440      1.1    pk 		vap->va_gid = 0;
    441  1.1.1.2  fvdl 		vap->va_size = vap->va_bytes = DEV_BSIZE;
    442  1.1.1.1  fvdl 		break;
    443  1.1.1.1  fvdl 
    444  1.1.1.2  fvdl 	case Pcurproc: {
    445  1.1.1.2  fvdl 		char buf[16];		/* should be enough */
    446  1.1.1.2  fvdl 		vap->va_nlink = 1;
    447  1.1.1.2  fvdl 		vap->va_uid = 0;
    448  1.1.1.2  fvdl 		vap->va_gid = 0;
    449  1.1.1.2  fvdl 		vap->va_size = vap->va_bytes =
    450  1.1.1.2  fvdl 		    sprintf(buf, "%ld", (long)curproc->p_pid);
    451  1.1.1.2  fvdl 		break;
    452  1.1.1.2  fvdl 	}
    453  1.1.1.2  fvdl 
    454  1.1.1.1  fvdl 	case Pproc:
    455  1.1.1.1  fvdl 		vap->va_nlink = 2;
    456  1.1.1.1  fvdl 		vap->va_uid = procp->p_ucred->cr_uid;
    457  1.1.1.1  fvdl 		vap->va_gid = procp->p_ucred->cr_gid;
    458  1.1.1.2  fvdl 		vap->va_size = vap->va_bytes = DEV_BSIZE;
    459  1.1.1.1  fvdl 		break;
    460  1.1.1.1  fvdl 
    461  1.1.1.1  fvdl 	case Pfile:
    462  1.1.1.1  fvdl 		error = EOPNOTSUPP;
    463  1.1.1.1  fvdl 		break;
    464  1.1.1.1  fvdl 
    465  1.1.1.1  fvdl 	case Pmem:
    466  1.1.1.1  fvdl 		vap->va_bytes = vap->va_size =
    467  1.1.1.1  fvdl 			ctob(procp->p_vmspace->vm_tsize +
    468  1.1.1.1  fvdl 				    procp->p_vmspace->vm_dsize +
    469  1.1.1.1  fvdl 				    procp->p_vmspace->vm_ssize);
    470  1.1.1.1  fvdl 		break;
    471  1.1.1.1  fvdl 
    472  1.1.1.1  fvdl 	case Pregs:
    473  1.1.1.2  fvdl 		vap->va_bytes = vap->va_size = sizeof(struct reg);
    474  1.1.1.2  fvdl 		break;
    475  1.1.1.2  fvdl 
    476  1.1.1.1  fvdl 	case Pfpregs:
    477  1.1.1.2  fvdl 		vap->va_bytes = vap->va_size = sizeof(struct fpreg);
    478  1.1.1.2  fvdl 		break;
    479  1.1.1.2  fvdl 
    480  1.1.1.1  fvdl 	case Pctl:
    481  1.1.1.1  fvdl 	case Pstatus:
    482  1.1.1.1  fvdl 	case Pnote:
    483  1.1.1.1  fvdl 	case Pnotepg:
    484  1.1.1.1  fvdl 		break;
    485  1.1.1.1  fvdl 
    486  1.1.1.1  fvdl 	default:
    487  1.1.1.1  fvdl 		panic("procfs_getattr");
    488      1.1    pk 	}
    489      1.1    pk 
    490  1.1.1.1  fvdl 	return (error);
    491  1.1.1.1  fvdl }
    492  1.1.1.1  fvdl 
    493  1.1.1.1  fvdl procfs_setattr(ap)
    494  1.1.1.2  fvdl 	struct vop_setattr_args /* {
    495  1.1.1.2  fvdl 		struct vnode *a_vp;
    496  1.1.1.2  fvdl 		struct vattr *a_vap;
    497  1.1.1.2  fvdl 		struct ucred *a_cred;
    498  1.1.1.2  fvdl 		struct proc *a_p;
    499  1.1.1.2  fvdl 	} */ *ap;
    500  1.1.1.1  fvdl {
    501  1.1.1.1  fvdl 	/*
    502  1.1.1.1  fvdl 	 * just fake out attribute setting
    503  1.1.1.1  fvdl 	 * it's not good to generate an error
    504  1.1.1.1  fvdl 	 * return, otherwise things like creat()
    505  1.1.1.1  fvdl 	 * will fail when they try to set the
    506  1.1.1.1  fvdl 	 * file length to 0.  worse, this means
    507  1.1.1.1  fvdl 	 * that echo $note > /proc/$pid/note will fail.
    508  1.1.1.1  fvdl 	 */
    509  1.1.1.1  fvdl 
    510  1.1.1.1  fvdl 	return (0);
    511  1.1.1.1  fvdl }
    512  1.1.1.1  fvdl 
    513  1.1.1.1  fvdl /*
    514  1.1.1.1  fvdl  * implement access checking.
    515  1.1.1.1  fvdl  *
    516  1.1.1.1  fvdl  * something very similar to this code is duplicated
    517  1.1.1.1  fvdl  * throughout the 4bsd kernel and should be moved
    518  1.1.1.1  fvdl  * into kern/vfs_subr.c sometime.
    519  1.1.1.1  fvdl  *
    520  1.1.1.1  fvdl  * actually, the check for super-user is slightly
    521  1.1.1.1  fvdl  * broken since it will allow read access to write-only
    522  1.1.1.1  fvdl  * objects.  this doesn't cause any particular trouble
    523  1.1.1.1  fvdl  * but does mean that the i/o entry points need to check
    524  1.1.1.1  fvdl  * that the operation really does make sense.
    525  1.1.1.1  fvdl  */
    526  1.1.1.1  fvdl procfs_access(ap)
    527  1.1.1.2  fvdl 	struct vop_access_args /* {
    528  1.1.1.2  fvdl 		struct vnode *a_vp;
    529  1.1.1.2  fvdl 		int a_mode;
    530  1.1.1.2  fvdl 		struct ucred *a_cred;
    531  1.1.1.2  fvdl 		struct proc *a_p;
    532  1.1.1.2  fvdl 	} */ *ap;
    533      1.1    pk {
    534  1.1.1.1  fvdl 	struct vattr *vap;
    535      1.1    pk 	struct vattr vattr;
    536      1.1    pk 	int error;
    537      1.1    pk 
    538      1.1    pk 	/*
    539      1.1    pk 	 * If you're the super-user,
    540      1.1    pk 	 * you always get access.
    541      1.1    pk 	 */
    542  1.1.1.2  fvdl 	if (ap->a_cred->cr_uid == 0)
    543      1.1    pk 		return (0);
    544  1.1.1.2  fvdl 
    545      1.1    pk 	vap = &vattr;
    546  1.1.1.1  fvdl 	if (error = VOP_GETATTR(ap->a_vp, vap, ap->a_cred, ap->a_p))
    547      1.1    pk 		return (error);
    548  1.1.1.1  fvdl 
    549      1.1    pk 	/*
    550      1.1    pk 	 * Access check is based on only one of owner, group, public.
    551      1.1    pk 	 * If not owner, then check group. If not a member of the
    552      1.1    pk 	 * group, then check public access.
    553      1.1    pk 	 */
    554  1.1.1.1  fvdl 	if (ap->a_cred->cr_uid != vap->va_uid) {
    555  1.1.1.1  fvdl 		gid_t *gp;
    556  1.1.1.1  fvdl 		int i;
    557  1.1.1.1  fvdl 
    558  1.1.1.2  fvdl 		ap->a_mode >>= 3;
    559  1.1.1.1  fvdl 		gp = ap->a_cred->cr_groups;
    560  1.1.1.1  fvdl 		for (i = 0; i < ap->a_cred->cr_ngroups; i++, gp++)
    561      1.1    pk 			if (vap->va_gid == *gp)
    562      1.1    pk 				goto found;
    563  1.1.1.1  fvdl 		ap->a_mode >>= 3;
    564      1.1    pk found:
    565      1.1    pk 		;
    566      1.1    pk 	}
    567  1.1.1.1  fvdl 
    568  1.1.1.1  fvdl 	if ((vap->va_mode & ap->a_mode) == ap->a_mode)
    569      1.1    pk 		return (0);
    570  1.1.1.1  fvdl 
    571      1.1    pk 	return (EACCES);
    572      1.1    pk }
    573      1.1    pk 
    574      1.1    pk /*
    575  1.1.1.1  fvdl  * lookup.  this is incredibly complicated in the
    576  1.1.1.1  fvdl  * general case, however for most pseudo-filesystems
    577  1.1.1.1  fvdl  * very little needs to be done.
    578  1.1.1.1  fvdl  *
    579  1.1.1.1  fvdl  * unless you want to get a migraine, just make sure your
    580  1.1.1.1  fvdl  * filesystem doesn't do any locking of its own.  otherwise
    581  1.1.1.1  fvdl  * read and inwardly digest ufs_lookup().
    582  1.1.1.1  fvdl  */
    583  1.1.1.1  fvdl procfs_lookup(ap)
    584  1.1.1.2  fvdl 	struct vop_lookup_args /* {
    585  1.1.1.2  fvdl 		struct vnode * a_dvp;
    586  1.1.1.2  fvdl 		struct vnode ** a_vpp;
    587  1.1.1.2  fvdl 		struct componentname * a_cnp;
    588  1.1.1.2  fvdl 	} */ *ap;
    589  1.1.1.1  fvdl {
    590  1.1.1.1  fvdl 	struct componentname *cnp = ap->a_cnp;
    591  1.1.1.1  fvdl 	struct vnode **vpp = ap->a_vpp;
    592  1.1.1.1  fvdl 	struct vnode *dvp = ap->a_dvp;
    593  1.1.1.1  fvdl 	char *pname = cnp->cn_nameptr;
    594  1.1.1.2  fvdl 	struct proc *curp = cnp->cn_proc;
    595  1.1.1.1  fvdl 	int error = 0;
    596  1.1.1.2  fvdl 	struct proc_target *pt;
    597  1.1.1.2  fvdl 	struct vnode *fvp;
    598      1.1    pk 	pid_t pid;
    599  1.1.1.1  fvdl 	struct pfsnode *pfs;
    600  1.1.1.2  fvdl 	struct proc *p;
    601  1.1.1.1  fvdl 	int i;
    602      1.1    pk 
    603  1.1.1.2  fvdl 	*vpp = NULL;
    604  1.1.1.2  fvdl 
    605  1.1.1.2  fvdl 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    606  1.1.1.2  fvdl 		return (EROFS);
    607  1.1.1.2  fvdl 
    608  1.1.1.1  fvdl 	if (cnp->cn_namelen == 1 && *pname == '.') {
    609  1.1.1.1  fvdl 		*vpp = dvp;
    610  1.1.1.1  fvdl 		VREF(dvp);
    611  1.1.1.2  fvdl 		/* vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, curp); */
    612  1.1.1.1  fvdl 		return (0);
    613  1.1.1.1  fvdl 	}
    614      1.1    pk 
    615  1.1.1.1  fvdl 	pfs = VTOPFS(dvp);
    616  1.1.1.1  fvdl 	switch (pfs->pfs_type) {
    617  1.1.1.1  fvdl 	case Proot:
    618  1.1.1.1  fvdl 		if (cnp->cn_flags & ISDOTDOT)
    619  1.1.1.1  fvdl 			return (EIO);
    620  1.1.1.1  fvdl 
    621  1.1.1.1  fvdl 		if (CNEQ(cnp, "curproc", 7))
    622  1.1.1.2  fvdl 			return (procfs_allocvp(dvp->v_mount, vpp, 0, Pcurproc));
    623  1.1.1.1  fvdl 
    624  1.1.1.2  fvdl 		pid = atopid(pname, cnp->cn_namelen);
    625  1.1.1.2  fvdl 		if (pid == NO_PID)
    626  1.1.1.2  fvdl 			break;
    627      1.1    pk 
    628  1.1.1.2  fvdl 		p = PFIND(pid);
    629  1.1.1.2  fvdl 		if (p == 0)
    630  1.1.1.2  fvdl 			break;
    631      1.1    pk 
    632  1.1.1.2  fvdl 		return (procfs_allocvp(dvp->v_mount, vpp, pid, Pproc));
    633      1.1    pk 
    634  1.1.1.1  fvdl 	case Pproc:
    635  1.1.1.2  fvdl 		if (cnp->cn_flags & ISDOTDOT)
    636  1.1.1.2  fvdl 			return (procfs_root(dvp->v_mount, vpp));
    637  1.1.1.1  fvdl 
    638  1.1.1.2  fvdl 		p = PFIND(pfs->pfs_pid);
    639  1.1.1.2  fvdl 		if (p == 0)
    640  1.1.1.2  fvdl 			break;
    641  1.1.1.2  fvdl 
    642  1.1.1.2  fvdl 		for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
    643  1.1.1.2  fvdl 			if (cnp->cn_namelen == pt->pt_namlen &&
    644  1.1.1.2  fvdl 			    bcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
    645  1.1.1.2  fvdl 			    (pt->pt_valid == NULL || (*pt->pt_valid)(p)))
    646  1.1.1.1  fvdl 				goto found;
    647  1.1.1.1  fvdl 		}
    648  1.1.1.2  fvdl 		break;
    649  1.1.1.1  fvdl 
    650  1.1.1.1  fvdl 	found:
    651  1.1.1.2  fvdl 		if (pt->pt_pfstype == Pfile) {
    652  1.1.1.2  fvdl 			fvp = procfs_findtextvp(p);
    653  1.1.1.2  fvdl 			/* We already checked that it exists. */
    654  1.1.1.2  fvdl 			VREF(fvp);
    655  1.1.1.2  fvdl 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, curp);
    656  1.1.1.2  fvdl 			*vpp = fvp;
    657  1.1.1.2  fvdl 			return (0);
    658  1.1.1.1  fvdl 		}
    659  1.1.1.2  fvdl 
    660  1.1.1.2  fvdl 		return (procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
    661  1.1.1.2  fvdl 		    pt->pt_pfstype));
    662  1.1.1.1  fvdl 
    663  1.1.1.1  fvdl 	default:
    664  1.1.1.1  fvdl 		return (ENOTDIR);
    665  1.1.1.1  fvdl 	}
    666  1.1.1.2  fvdl 
    667  1.1.1.2  fvdl 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
    668  1.1.1.2  fvdl }
    669  1.1.1.2  fvdl 
    670  1.1.1.2  fvdl int
    671  1.1.1.2  fvdl procfs_validfile(p)
    672  1.1.1.2  fvdl 	struct proc *p;
    673  1.1.1.2  fvdl {
    674  1.1.1.2  fvdl 
    675  1.1.1.2  fvdl 	return (procfs_findtextvp(p) != NULLVP);
    676      1.1    pk }
    677      1.1    pk 
    678  1.1.1.1  fvdl /*
    679  1.1.1.1  fvdl  * readdir returns directory entries from pfsnode (vp).
    680  1.1.1.1  fvdl  *
    681  1.1.1.1  fvdl  * the strategy here with procfs is to generate a single
    682  1.1.1.1  fvdl  * directory entry at a time (struct pfsdent) and then
    683  1.1.1.1  fvdl  * copy that out to userland using uiomove.  a more efficent
    684  1.1.1.1  fvdl  * though more complex implementation, would try to minimize
    685  1.1.1.1  fvdl  * the number of calls to uiomove().  for procfs, this is
    686  1.1.1.1  fvdl  * hardly worth the added code complexity.
    687  1.1.1.1  fvdl  *
    688  1.1.1.1  fvdl  * this should just be done through read()
    689  1.1.1.1  fvdl  */
    690  1.1.1.1  fvdl procfs_readdir(ap)
    691  1.1.1.2  fvdl 	struct vop_readdir_args /* {
    692  1.1.1.2  fvdl 		struct vnode *a_vp;
    693  1.1.1.2  fvdl 		struct uio *a_uio;
    694  1.1.1.2  fvdl 		struct ucred *a_cred;
    695  1.1.1.2  fvdl 		int *a_eofflag;
    696  1.1.1.2  fvdl 		u_long *a_cookies;
    697  1.1.1.2  fvdl 		int a_ncookies;
    698  1.1.1.2  fvdl 	} */ *ap;
    699  1.1.1.1  fvdl {
    700  1.1.1.1  fvdl 	struct uio *uio = ap->a_uio;
    701  1.1.1.1  fvdl 	struct pfsdent d;
    702  1.1.1.1  fvdl 	struct pfsdent *dp = &d;
    703  1.1.1.1  fvdl 	struct pfsnode *pfs;
    704  1.1.1.1  fvdl 	int error;
    705  1.1.1.1  fvdl 	int count;
    706  1.1.1.1  fvdl 	int i;
    707  1.1.1.1  fvdl 
    708  1.1.1.2  fvdl 	/*
    709  1.1.1.2  fvdl 	 * We don't allow exporting procfs mounts, and currently local
    710  1.1.1.2  fvdl 	 * requests do not need cookies.
    711  1.1.1.2  fvdl 	 */
    712  1.1.1.2  fvdl 	if (ap->a_ncookies)
    713  1.1.1.2  fvdl 		panic("procfs_readdir: not hungry");
    714  1.1.1.2  fvdl 
    715  1.1.1.1  fvdl 	pfs = VTOPFS(ap->a_vp);
    716  1.1.1.1  fvdl 
    717  1.1.1.1  fvdl 	if (uio->uio_resid < UIO_MX)
    718  1.1.1.1  fvdl 		return (EINVAL);
    719  1.1.1.1  fvdl 	if (uio->uio_offset & (UIO_MX-1))
    720  1.1.1.1  fvdl 		return (EINVAL);
    721  1.1.1.1  fvdl 	if (uio->uio_offset < 0)
    722      1.1    pk 		return (EINVAL);
    723      1.1    pk 
    724  1.1.1.1  fvdl 	error = 0;
    725      1.1    pk 	count = 0;
    726  1.1.1.1  fvdl 	i = uio->uio_offset / UIO_MX;
    727      1.1    pk 
    728  1.1.1.1  fvdl 	switch (pfs->pfs_type) {
    729  1.1.1.1  fvdl 	/*
    730  1.1.1.1  fvdl 	 * this is for the process-specific sub-directories.
    731  1.1.1.1  fvdl 	 * all that is needed to is copy out all the entries
    732  1.1.1.1  fvdl 	 * from the procent[] table (top of this file).
    733  1.1.1.1  fvdl 	 */
    734  1.1.1.1  fvdl 	case Pproc: {
    735  1.1.1.2  fvdl 		struct proc *p;
    736  1.1.1.2  fvdl 		struct proc_target *pt;
    737  1.1.1.1  fvdl 
    738  1.1.1.2  fvdl 		p = PFIND(pfs->pfs_pid);
    739  1.1.1.2  fvdl 		if (p == NULL)
    740  1.1.1.2  fvdl 			break;
    741  1.1.1.2  fvdl 
    742  1.1.1.2  fvdl 		for (pt = &proc_targets[i];
    743  1.1.1.2  fvdl 		     uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
    744  1.1.1.2  fvdl 			if (pt->pt_valid && (*pt->pt_valid)(p) == 0)
    745  1.1.1.2  fvdl 				continue;
    746  1.1.1.1  fvdl 
    747  1.1.1.1  fvdl 			dp->d_reclen = UIO_MX;
    748  1.1.1.2  fvdl 			dp->d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype);
    749  1.1.1.2  fvdl 			dp->d_namlen = pt->pt_namlen;
    750  1.1.1.2  fvdl 			bcopy(pt->pt_name, dp->d_name, pt->pt_namlen + 1);
    751  1.1.1.2  fvdl 			dp->d_type = pt->pt_type;
    752  1.1.1.2  fvdl 
    753  1.1.1.2  fvdl 			if (error = uiomove((caddr_t)dp, UIO_MX, uio))
    754  1.1.1.1  fvdl 				break;
    755  1.1.1.1  fvdl 		}
    756  1.1.1.1  fvdl 
    757  1.1.1.1  fvdl 	    	break;
    758  1.1.1.1  fvdl 	    }
    759  1.1.1.1  fvdl 
    760  1.1.1.1  fvdl 	/*
    761  1.1.1.1  fvdl 	 * this is for the root of the procfs filesystem
    762  1.1.1.1  fvdl 	 * what is needed is a special entry for "curproc"
    763  1.1.1.1  fvdl 	 * followed by an entry for each process on allproc
    764  1.1.1.1  fvdl #ifdef PROCFS_ZOMBIE
    765  1.1.1.1  fvdl 	 * and zombproc.
    766      1.1    pk #endif
    767  1.1.1.1  fvdl 	 */
    768      1.1    pk 
    769  1.1.1.1  fvdl 	case Proot: {
    770  1.1.1.1  fvdl #ifdef PROCFS_ZOMBIE
    771  1.1.1.1  fvdl 		int doingzomb = 0;
    772  1.1.1.1  fvdl #endif
    773  1.1.1.2  fvdl 		int pcnt = 0;
    774  1.1.1.2  fvdl 		volatile struct proc *p = allproc.lh_first;
    775  1.1.1.1  fvdl 
    776  1.1.1.2  fvdl 	again:
    777  1.1.1.2  fvdl 		for (; p && uio->uio_resid >= UIO_MX; i++, pcnt++) {
    778  1.1.1.1  fvdl 			bzero((char *) dp, UIO_MX);
    779  1.1.1.1  fvdl 			dp->d_reclen = UIO_MX;
    780  1.1.1.1  fvdl 
    781  1.1.1.1  fvdl 			switch (i) {
    782  1.1.1.2  fvdl 			case 0:		/* `.' */
    783  1.1.1.2  fvdl 			case 1:		/* `..' */
    784  1.1.1.2  fvdl 				dp->d_fileno = PROCFS_FILENO(0, Proot);
    785  1.1.1.2  fvdl 				dp->d_namlen = i + 1;
    786  1.1.1.2  fvdl 				bcopy("..", dp->d_name, dp->d_namlen);
    787  1.1.1.2  fvdl 				dp->d_name[i + 1] = '\0';
    788  1.1.1.2  fvdl 				dp->d_type = DT_DIR;
    789  1.1.1.1  fvdl 				break;
    790  1.1.1.1  fvdl 
    791  1.1.1.2  fvdl 			case 2:
    792  1.1.1.2  fvdl 				dp->d_fileno = PROCFS_FILENO(0, Pcurproc);
    793  1.1.1.2  fvdl 				dp->d_namlen = 7;
    794  1.1.1.2  fvdl 				bcopy("curproc", dp->d_name, 8);
    795  1.1.1.2  fvdl 				dp->d_type = DT_LNK;
    796  1.1.1.2  fvdl 				break;
    797  1.1.1.1  fvdl 
    798  1.1.1.2  fvdl 			default:
    799  1.1.1.2  fvdl 				while (pcnt < i) {
    800  1.1.1.2  fvdl 					pcnt++;
    801  1.1.1.2  fvdl 					p = p->p_list.le_next;
    802  1.1.1.2  fvdl 					if (!p)
    803  1.1.1.2  fvdl 						goto done;
    804  1.1.1.1  fvdl 				}
    805  1.1.1.2  fvdl 				dp->d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
    806  1.1.1.2  fvdl 				dp->d_namlen = sprintf(dp->d_name, "%ld",
    807  1.1.1.2  fvdl 				    (long)p->p_pid);
    808  1.1.1.2  fvdl 				dp->d_type = DT_REG;
    809  1.1.1.2  fvdl 				p = p->p_list.le_next;
    810  1.1.1.1  fvdl 				break;
    811      1.1    pk 			}
    812  1.1.1.2  fvdl 
    813  1.1.1.2  fvdl 			if (error = uiomove((caddr_t)dp, UIO_MX, uio))
    814  1.1.1.1  fvdl 				break;
    815      1.1    pk 		}
    816  1.1.1.2  fvdl 	done:
    817  1.1.1.2  fvdl 
    818  1.1.1.2  fvdl #ifdef PROCFS_ZOMBIE
    819  1.1.1.2  fvdl 		if (p == 0 && doingzomb == 0) {
    820  1.1.1.2  fvdl 			doingzomb = 1;
    821  1.1.1.2  fvdl 			p = zombproc.lh_first;
    822  1.1.1.2  fvdl 			goto again;
    823  1.1.1.2  fvdl 		}
    824  1.1.1.2  fvdl #endif
    825      1.1    pk 
    826  1.1.1.1  fvdl 		break;
    827  1.1.1.1  fvdl 
    828  1.1.1.1  fvdl 	    }
    829  1.1.1.1  fvdl 
    830  1.1.1.1  fvdl 	default:
    831  1.1.1.1  fvdl 		error = ENOTDIR;
    832  1.1.1.1  fvdl 		break;
    833      1.1    pk 	}
    834      1.1    pk 
    835  1.1.1.1  fvdl 	uio->uio_offset = i * UIO_MX;
    836      1.1    pk 
    837  1.1.1.1  fvdl 	return (error);
    838      1.1    pk }
    839      1.1    pk 
    840      1.1    pk /*
    841  1.1.1.2  fvdl  * readlink reads the link of `curproc'
    842  1.1.1.2  fvdl  */
    843  1.1.1.2  fvdl procfs_readlink(ap)
    844  1.1.1.2  fvdl 	struct vop_readlink_args *ap;
    845  1.1.1.2  fvdl {
    846  1.1.1.2  fvdl 	struct uio *uio = ap->a_uio;
    847  1.1.1.2  fvdl 	char buf[16];		/* should be enough */
    848  1.1.1.2  fvdl 	int len;
    849  1.1.1.2  fvdl 
    850  1.1.1.2  fvdl 	if (VTOPFS(ap->a_vp)->pfs_fileno != PROCFS_FILENO(0, Pcurproc))
    851  1.1.1.2  fvdl 		return (EINVAL);
    852  1.1.1.2  fvdl 
    853  1.1.1.2  fvdl 	len = sprintf(buf, "%ld", (long)curproc->p_pid);
    854  1.1.1.2  fvdl 
    855  1.1.1.2  fvdl 	return (uiomove((caddr_t)buf, len, ap->a_uio));
    856  1.1.1.2  fvdl }
    857  1.1.1.2  fvdl 
    858  1.1.1.2  fvdl /*
    859  1.1.1.1  fvdl  * convert decimal ascii to pid_t
    860      1.1    pk  */
    861  1.1.1.1  fvdl static pid_t
    862  1.1.1.1  fvdl atopid(b, len)
    863  1.1.1.1  fvdl 	const char *b;
    864  1.1.1.1  fvdl 	u_int len;
    865      1.1    pk {
    866  1.1.1.1  fvdl 	pid_t p = 0;
    867      1.1    pk 
    868      1.1    pk 	while (len--) {
    869  1.1.1.1  fvdl 		char c = *b++;
    870      1.1    pk 		if (c < '0' || c > '9')
    871  1.1.1.1  fvdl 			return (NO_PID);
    872  1.1.1.1  fvdl 		p = 10 * p + (c - '0');
    873  1.1.1.1  fvdl 		if (p > PID_MAX)
    874  1.1.1.1  fvdl 			return (NO_PID);
    875      1.1    pk 	}
    876  1.1.1.1  fvdl 
    877  1.1.1.1  fvdl 	return (p);
    878      1.1    pk }
    879  1.1.1.1  fvdl 
    880  1.1.1.1  fvdl /*
    881  1.1.1.1  fvdl  * procfs vnode operations.
    882  1.1.1.1  fvdl  */
    883  1.1.1.1  fvdl int (**procfs_vnodeop_p)();
    884  1.1.1.1  fvdl struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
    885  1.1.1.1  fvdl 	{ &vop_default_desc, vn_default_error },
    886  1.1.1.1  fvdl 	{ &vop_lookup_desc, procfs_lookup },		/* lookup */
    887  1.1.1.1  fvdl 	{ &vop_create_desc, procfs_create },		/* create */
    888  1.1.1.1  fvdl 	{ &vop_mknod_desc, procfs_mknod },		/* mknod */
    889  1.1.1.1  fvdl 	{ &vop_open_desc, procfs_open },		/* open */
    890  1.1.1.1  fvdl 	{ &vop_close_desc, procfs_close },		/* close */
    891  1.1.1.1  fvdl 	{ &vop_access_desc, procfs_access },		/* access */
    892  1.1.1.1  fvdl 	{ &vop_getattr_desc, procfs_getattr },		/* getattr */
    893  1.1.1.1  fvdl 	{ &vop_setattr_desc, procfs_setattr },		/* setattr */
    894  1.1.1.1  fvdl 	{ &vop_read_desc, procfs_read },		/* read */
    895  1.1.1.1  fvdl 	{ &vop_write_desc, procfs_write },		/* write */
    896  1.1.1.1  fvdl 	{ &vop_ioctl_desc, procfs_ioctl },		/* ioctl */
    897  1.1.1.1  fvdl 	{ &vop_select_desc, procfs_select },		/* select */
    898  1.1.1.1  fvdl 	{ &vop_mmap_desc, procfs_mmap },		/* mmap */
    899  1.1.1.2  fvdl 	{ &vop_revoke_desc, procfs_revoke },		/* revoke */
    900  1.1.1.1  fvdl 	{ &vop_fsync_desc, procfs_fsync },		/* fsync */
    901  1.1.1.1  fvdl 	{ &vop_seek_desc, procfs_seek },		/* seek */
    902  1.1.1.1  fvdl 	{ &vop_remove_desc, procfs_remove },		/* remove */
    903  1.1.1.1  fvdl 	{ &vop_link_desc, procfs_link },		/* link */
    904  1.1.1.1  fvdl 	{ &vop_rename_desc, procfs_rename },		/* rename */
    905  1.1.1.1  fvdl 	{ &vop_mkdir_desc, procfs_mkdir },		/* mkdir */
    906  1.1.1.1  fvdl 	{ &vop_rmdir_desc, procfs_rmdir },		/* rmdir */
    907  1.1.1.1  fvdl 	{ &vop_symlink_desc, procfs_symlink },		/* symlink */
    908  1.1.1.1  fvdl 	{ &vop_readdir_desc, procfs_readdir },		/* readdir */
    909  1.1.1.1  fvdl 	{ &vop_readlink_desc, procfs_readlink },	/* readlink */
    910  1.1.1.1  fvdl 	{ &vop_abortop_desc, procfs_abortop },		/* abortop */
    911  1.1.1.1  fvdl 	{ &vop_inactive_desc, procfs_inactive },	/* inactive */
    912  1.1.1.1  fvdl 	{ &vop_reclaim_desc, procfs_reclaim },		/* reclaim */
    913  1.1.1.1  fvdl 	{ &vop_lock_desc, procfs_lock },		/* lock */
    914  1.1.1.1  fvdl 	{ &vop_unlock_desc, procfs_unlock },		/* unlock */
    915  1.1.1.1  fvdl 	{ &vop_bmap_desc, procfs_bmap },		/* bmap */
    916  1.1.1.1  fvdl 	{ &vop_strategy_desc, procfs_strategy },	/* strategy */
    917  1.1.1.1  fvdl 	{ &vop_print_desc, procfs_print },		/* print */
    918  1.1.1.1  fvdl 	{ &vop_islocked_desc, procfs_islocked },	/* islocked */
    919  1.1.1.1  fvdl 	{ &vop_pathconf_desc, procfs_pathconf },	/* pathconf */
    920  1.1.1.1  fvdl 	{ &vop_advlock_desc, procfs_advlock },		/* advlock */
    921  1.1.1.1  fvdl 	{ &vop_blkatoff_desc, procfs_blkatoff },	/* blkatoff */
    922  1.1.1.1  fvdl 	{ &vop_valloc_desc, procfs_valloc },		/* valloc */
    923  1.1.1.1  fvdl 	{ &vop_vfree_desc, procfs_vfree },		/* vfree */
    924  1.1.1.1  fvdl 	{ &vop_truncate_desc, procfs_truncate },	/* truncate */
    925  1.1.1.1  fvdl 	{ &vop_update_desc, procfs_update },		/* update */
    926  1.1.1.1  fvdl 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
    927  1.1.1.1  fvdl };
    928  1.1.1.1  fvdl struct vnodeopv_desc procfs_vnodeop_opv_desc =
    929  1.1.1.1  fvdl 	{ &procfs_vnodeop_p, procfs_vnodeop_entries };
    930