Home | History | Annotate | Line # | Download | only in procfs
procfs_vnops.c revision 1.82.2.2
      1 /*	$NetBSD: procfs_vnops.c,v 1.82.2.2 2002/01/10 20:01:45 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993 Jan-Simon Pendry
      5  * Copyright (c) 1993, 1995
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Jan-Simon Pendry.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  *	@(#)procfs_vnops.c	8.18 (Berkeley) 5/21/95
     40  */
     41 
     42 /*
     43  * procfs vnode interface
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: procfs_vnops.c,v 1.82.2.2 2002/01/10 20:01:45 thorpej Exp $");
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/time.h>
     52 #include <sys/kernel.h>
     53 #include <sys/file.h>
     54 #include <sys/proc.h>
     55 #include <sys/vnode.h>
     56 #include <sys/namei.h>
     57 #include <sys/malloc.h>
     58 #include <sys/mount.h>
     59 #include <sys/dirent.h>
     60 #include <sys/resourcevar.h>
     61 #include <sys/stat.h>
     62 
     63 #include <uvm/uvm_extern.h>	/* for PAGE_SIZE */
     64 
     65 #include <machine/reg.h>
     66 
     67 #include <miscfs/genfs/genfs.h>
     68 #include <miscfs/procfs/procfs.h>
     69 
     70 /*
     71  * Vnode Operations.
     72  *
     73  */
     74 
     75 static int procfs_validfile_linux __P((struct proc *, struct mount *));
     76 
     77 /*
     78  * This is a list of the valid names in the
     79  * process-specific sub-directories.  It is
     80  * used in procfs_lookup and procfs_readdir
     81  */
     82 const struct proc_target {
     83 	u_char	pt_type;
     84 	u_char	pt_namlen;
     85 	char	*pt_name;
     86 	pfstype	pt_pfstype;
     87 	int	(*pt_valid) __P((struct proc *, struct mount *));
     88 } proc_targets[] = {
     89 #define N(s) sizeof(s)-1, s
     90 	/*	  name		type		validp */
     91 	{ DT_DIR, N("."),	Pproc,		NULL },
     92 	{ DT_DIR, N(".."),	Proot,		NULL },
     93 	{ DT_REG, N("file"),	Pfile,		procfs_validfile },
     94 	{ DT_REG, N("mem"),	Pmem,		NULL },
     95 	{ DT_REG, N("regs"),	Pregs,		procfs_validregs },
     96 	{ DT_REG, N("fpregs"),	Pfpregs,	procfs_validfpregs },
     97 	{ DT_REG, N("ctl"),	Pctl,		NULL },
     98 	{ DT_REG, N("status"),	Pstatus,	NULL },
     99 	{ DT_REG, N("note"),	Pnote,		NULL },
    100 	{ DT_REG, N("notepg"),	Pnotepg,	NULL },
    101 	{ DT_REG, N("map"),	Pmap,		procfs_validmap },
    102 	{ DT_REG, N("maps"),	Pmaps,		procfs_validmap },
    103 	{ DT_REG, N("cmdline"), Pcmdline,	NULL },
    104 	{ DT_REG, N("exe"),	Pfile,		procfs_validfile_linux },
    105 #ifdef __HAVE_PROCFS_MACHDEP
    106 	PROCFS_MACHDEP_NODETYPE_DEFNS
    107 #endif
    108 #undef N
    109 };
    110 static int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
    111 
    112 /*
    113  * List of files in the root directory. Note: the validate function will
    114  * be called with p == NULL for these ones.
    115  */
    116 struct proc_target proc_root_targets[] = {
    117 #define N(s) sizeof(s)-1, s
    118 	/*	  name		    type	    validp */
    119 	{ DT_REG, N("meminfo"),     Pmeminfo,        procfs_validfile_linux },
    120 	{ DT_REG, N("cpuinfo"),     Pcpuinfo,        procfs_validfile_linux },
    121 #undef N
    122 };
    123 static int nproc_root_targets =
    124     sizeof(proc_root_targets) / sizeof(proc_root_targets[0]);
    125 
    126 int	procfs_lookup	__P((void *));
    127 #define	procfs_create	genfs_eopnotsupp_rele
    128 #define	procfs_mknod	genfs_eopnotsupp_rele
    129 int	procfs_open	__P((void *));
    130 int	procfs_close	__P((void *));
    131 int	procfs_access	__P((void *));
    132 int	procfs_getattr	__P((void *));
    133 int	procfs_setattr	__P((void *));
    134 #define	procfs_read	procfs_rw
    135 #define	procfs_write	procfs_rw
    136 #define	procfs_fcntl	genfs_fcntl
    137 #define	procfs_ioctl	genfs_enoioctl
    138 #define	procfs_poll	genfs_poll
    139 #define procfs_revoke	genfs_revoke
    140 #define	procfs_fsync	genfs_nullop
    141 #define	procfs_seek	genfs_nullop
    142 #define	procfs_remove	genfs_eopnotsupp_rele
    143 int	procfs_link	__P((void *));
    144 #define	procfs_rename	genfs_eopnotsupp_rele
    145 #define	procfs_mkdir	genfs_eopnotsupp_rele
    146 #define	procfs_rmdir	genfs_eopnotsupp_rele
    147 int	procfs_symlink	__P((void *));
    148 int	procfs_readdir	__P((void *));
    149 int	procfs_readlink	__P((void *));
    150 #define	procfs_abortop	genfs_abortop
    151 int	procfs_inactive	__P((void *));
    152 int	procfs_reclaim	__P((void *));
    153 #define	procfs_lock	genfs_lock
    154 #define	procfs_unlock	genfs_unlock
    155 #define	procfs_bmap	genfs_badop
    156 #define	procfs_strategy	genfs_badop
    157 int	procfs_print	__P((void *));
    158 int	procfs_pathconf	__P((void *));
    159 #define	procfs_islocked	genfs_islocked
    160 #define	procfs_advlock	genfs_einval
    161 #define	procfs_blkatoff	genfs_eopnotsupp
    162 #define	procfs_valloc	genfs_eopnotsupp
    163 #define	procfs_vfree	genfs_nullop
    164 #define	procfs_truncate	genfs_eopnotsupp
    165 #define	procfs_update	genfs_nullop
    166 #define	procfs_bwrite	genfs_eopnotsupp
    167 #define procfs_putpages	genfs_null_putpages
    168 
    169 static pid_t atopid __P((const char *, u_int));
    170 
    171 /*
    172  * procfs vnode operations.
    173  */
    174 int (**procfs_vnodeop_p) __P((void *));
    175 const struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
    176 	{ &vop_default_desc, vn_default_error },
    177 	{ &vop_lookup_desc, procfs_lookup },		/* lookup */
    178 	{ &vop_create_desc, procfs_create },		/* create */
    179 	{ &vop_mknod_desc, procfs_mknod },		/* mknod */
    180 	{ &vop_open_desc, procfs_open },		/* open */
    181 	{ &vop_close_desc, procfs_close },		/* close */
    182 	{ &vop_access_desc, procfs_access },		/* access */
    183 	{ &vop_getattr_desc, procfs_getattr },		/* getattr */
    184 	{ &vop_setattr_desc, procfs_setattr },		/* setattr */
    185 	{ &vop_read_desc, procfs_read },		/* read */
    186 	{ &vop_write_desc, procfs_write },		/* write */
    187 	{ &vop_fcntl_desc, procfs_fcntl },		/* fcntl */
    188 	{ &vop_ioctl_desc, procfs_ioctl },		/* ioctl */
    189 	{ &vop_poll_desc, procfs_poll },		/* poll */
    190 	{ &vop_revoke_desc, procfs_revoke },		/* revoke */
    191 	{ &vop_fsync_desc, procfs_fsync },		/* fsync */
    192 	{ &vop_seek_desc, procfs_seek },		/* seek */
    193 	{ &vop_remove_desc, procfs_remove },		/* remove */
    194 	{ &vop_link_desc, procfs_link },		/* link */
    195 	{ &vop_rename_desc, procfs_rename },		/* rename */
    196 	{ &vop_mkdir_desc, procfs_mkdir },		/* mkdir */
    197 	{ &vop_rmdir_desc, procfs_rmdir },		/* rmdir */
    198 	{ &vop_symlink_desc, procfs_symlink },		/* symlink */
    199 	{ &vop_readdir_desc, procfs_readdir },		/* readdir */
    200 	{ &vop_readlink_desc, procfs_readlink },	/* readlink */
    201 	{ &vop_abortop_desc, procfs_abortop },		/* abortop */
    202 	{ &vop_inactive_desc, procfs_inactive },	/* inactive */
    203 	{ &vop_reclaim_desc, procfs_reclaim },		/* reclaim */
    204 	{ &vop_lock_desc, procfs_lock },		/* lock */
    205 	{ &vop_unlock_desc, procfs_unlock },		/* unlock */
    206 	{ &vop_bmap_desc, procfs_bmap },		/* bmap */
    207 	{ &vop_strategy_desc, procfs_strategy },	/* strategy */
    208 	{ &vop_print_desc, procfs_print },		/* print */
    209 	{ &vop_islocked_desc, procfs_islocked },	/* islocked */
    210 	{ &vop_pathconf_desc, procfs_pathconf },	/* pathconf */
    211 	{ &vop_advlock_desc, procfs_advlock },		/* advlock */
    212 	{ &vop_blkatoff_desc, procfs_blkatoff },	/* blkatoff */
    213 	{ &vop_valloc_desc, procfs_valloc },		/* valloc */
    214 	{ &vop_vfree_desc, procfs_vfree },		/* vfree */
    215 	{ &vop_truncate_desc, procfs_truncate },	/* truncate */
    216 	{ &vop_update_desc, procfs_update },		/* update */
    217 	{ &vop_putpages_desc, procfs_putpages },	/* putpages */
    218 	{ NULL, NULL }
    219 };
    220 const struct vnodeopv_desc procfs_vnodeop_opv_desc =
    221 	{ &procfs_vnodeop_p, procfs_vnodeop_entries };
    222 /*
    223  * set things up for doing i/o on
    224  * the pfsnode (vp).  (vp) is locked
    225  * on entry, and should be left locked
    226  * on exit.
    227  *
    228  * for procfs we don't need to do anything
    229  * in particular for i/o.  all that is done
    230  * is to support exclusive open on process
    231  * memory images.
    232  */
    233 int
    234 procfs_open(v)
    235 	void *v;
    236 {
    237 	struct vop_open_args /* {
    238 		struct vnode *a_vp;
    239 		int  a_mode;
    240 		struct ucred *a_cred;
    241 		struct proc *a_p;
    242 	} */ *ap = v;
    243 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    244 	struct proc *p1, *p2;
    245 
    246 	p1 = ap->a_p;				/* tracer */
    247 	p2 = PFIND(pfs->pfs_pid);		/* traced */
    248 
    249 	if (p2 == NULL)
    250 		return (ENOENT);		/* was ESRCH, jsp */
    251 
    252 	switch (pfs->pfs_type) {
    253 	case Pmem:
    254 		if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
    255 		    ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
    256 			return (EBUSY);
    257 
    258 		if (procfs_checkioperm(p1, p2) != 0)
    259 			return (EPERM);
    260 
    261 		if (ap->a_mode & FWRITE)
    262 			pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
    263 
    264 		return (0);
    265 
    266 	default:
    267 		break;
    268 	}
    269 
    270 	return (0);
    271 }
    272 
    273 /*
    274  * close the pfsnode (vp) after doing i/o.
    275  * (vp) is not locked on entry or exit.
    276  *
    277  * nothing to do for procfs other than undo
    278  * any exclusive open flag (see _open above).
    279  */
    280 int
    281 procfs_close(v)
    282 	void *v;
    283 {
    284 	struct vop_close_args /* {
    285 		struct vnode *a_vp;
    286 		int  a_fflag;
    287 		struct ucred *a_cred;
    288 		struct proc *a_p;
    289 	} */ *ap = v;
    290 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    291 
    292 	switch (pfs->pfs_type) {
    293 	case Pmem:
    294 		if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
    295 			pfs->pfs_flags &= ~(FWRITE|O_EXCL);
    296 		break;
    297 
    298 	default:
    299 		break;
    300 	}
    301 
    302 	return (0);
    303 }
    304 
    305 /*
    306  * _inactive is called when the pfsnode
    307  * is vrele'd and the reference count goes
    308  * to zero.  (vp) will be on the vnode free
    309  * list, so to get it back vget() must be
    310  * used.
    311  *
    312  * for procfs, check if the process is still
    313  * alive and if it isn't then just throw away
    314  * the vnode by calling vgone().  this may
    315  * be overkill and a waste of time since the
    316  * chances are that the process will still be
    317  * there and PFIND is not free.
    318  *
    319  * (vp) is locked on entry, but must be unlocked on exit.
    320  */
    321 int
    322 procfs_inactive(v)
    323 	void *v;
    324 {
    325 	struct vop_inactive_args /* {
    326 		struct vnode *a_vp;
    327 		struct proc *a_p;
    328 	} */ *ap = v;
    329 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    330 
    331 	VOP_UNLOCK(ap->a_vp, 0);
    332 	if (PFIND(pfs->pfs_pid) == 0)
    333 		vgone(ap->a_vp);
    334 
    335 	return (0);
    336 }
    337 
    338 /*
    339  * _reclaim is called when getnewvnode()
    340  * wants to make use of an entry on the vnode
    341  * free list.  at this time the filesystem needs
    342  * to free any private data and remove the node
    343  * from any private lists.
    344  */
    345 int
    346 procfs_reclaim(v)
    347 	void *v;
    348 {
    349 	struct vop_reclaim_args /* {
    350 		struct vnode *a_vp;
    351 	} */ *ap = v;
    352 
    353 	return (procfs_freevp(ap->a_vp));
    354 }
    355 
    356 /*
    357  * Return POSIX pathconf information applicable to special devices.
    358  */
    359 int
    360 procfs_pathconf(v)
    361 	void *v;
    362 {
    363 	struct vop_pathconf_args /* {
    364 		struct vnode *a_vp;
    365 		int a_name;
    366 		register_t *a_retval;
    367 	} */ *ap = v;
    368 
    369 	switch (ap->a_name) {
    370 	case _PC_LINK_MAX:
    371 		*ap->a_retval = LINK_MAX;
    372 		return (0);
    373 	case _PC_MAX_CANON:
    374 		*ap->a_retval = MAX_CANON;
    375 		return (0);
    376 	case _PC_MAX_INPUT:
    377 		*ap->a_retval = MAX_INPUT;
    378 		return (0);
    379 	case _PC_PIPE_BUF:
    380 		*ap->a_retval = PIPE_BUF;
    381 		return (0);
    382 	case _PC_CHOWN_RESTRICTED:
    383 		*ap->a_retval = 1;
    384 		return (0);
    385 	case _PC_VDISABLE:
    386 		*ap->a_retval = _POSIX_VDISABLE;
    387 		return (0);
    388 	case _PC_SYNC_IO:
    389 		*ap->a_retval = 1;
    390 		return (0);
    391 	default:
    392 		return (EINVAL);
    393 	}
    394 	/* NOTREACHED */
    395 }
    396 
    397 /*
    398  * _print is used for debugging.
    399  * just print a readable description
    400  * of (vp).
    401  */
    402 int
    403 procfs_print(v)
    404 	void *v;
    405 {
    406 	struct vop_print_args /* {
    407 		struct vnode *a_vp;
    408 	} */ *ap = v;
    409 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    410 
    411 	printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
    412 	    pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
    413 	return 0;
    414 }
    415 
    416 int
    417 procfs_link(v)
    418 	void *v;
    419 {
    420 	struct vop_link_args /* {
    421 		struct vnode *a_dvp;
    422 		struct vnode *a_vp;
    423 		struct componentname *a_cnp;
    424 	} */ *ap = v;
    425 
    426 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    427 	vput(ap->a_dvp);
    428 	return (EROFS);
    429 }
    430 
    431 int
    432 procfs_symlink(v)
    433 	void *v;
    434 {
    435 	struct vop_symlink_args /* {
    436 		struct vnode *a_dvp;
    437 		struct vnode **a_vpp;
    438 		struct componentname *a_cnp;
    439 		struct vattr *a_vap;
    440 		char *a_target;
    441 	} */ *ap = v;
    442 
    443 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    444 	vput(ap->a_dvp);
    445 	return (EROFS);
    446 }
    447 
    448 /*
    449  * Invent attributes for pfsnode (vp) and store
    450  * them in (vap).
    451  * Directories lengths are returned as zero since
    452  * any real length would require the genuine size
    453  * to be computed, and nothing cares anyway.
    454  *
    455  * this is relatively minimal for procfs.
    456  */
    457 int
    458 procfs_getattr(v)
    459 	void *v;
    460 {
    461 	struct vop_getattr_args /* {
    462 		struct vnode *a_vp;
    463 		struct vattr *a_vap;
    464 		struct ucred *a_cred;
    465 		struct proc *a_p;
    466 	} */ *ap = v;
    467 	struct pfsnode *pfs = VTOPFS(ap->a_vp);
    468 	struct vattr *vap = ap->a_vap;
    469 	struct proc *procp;
    470 	struct timeval tv;
    471 	int error;
    472 
    473 	/* first check the process still exists */
    474 	switch (pfs->pfs_type) {
    475 	case Proot:
    476 	case Pcurproc:
    477 	case Pself:
    478 		procp = 0;
    479 		break;
    480 
    481 	default:
    482 		procp = PFIND(pfs->pfs_pid);
    483 		if (procp == 0)
    484 			return (ENOENT);
    485 		break;
    486 	}
    487 
    488 	error = 0;
    489 
    490 	/* start by zeroing out the attributes */
    491 	VATTR_NULL(vap);
    492 
    493 	/* next do all the common fields */
    494 	vap->va_type = ap->a_vp->v_type;
    495 	vap->va_mode = pfs->pfs_mode;
    496 	vap->va_fileid = pfs->pfs_fileno;
    497 	vap->va_flags = 0;
    498 	vap->va_blocksize = PAGE_SIZE;
    499 
    500 	/*
    501 	 * Make all times be current TOD.
    502 	 * It would be possible to get the process start
    503 	 * time from the p_stat structure, but there's
    504 	 * no "file creation" time stamp anyway, and the
    505 	 * p_stat structure is not addressible if u. gets
    506 	 * swapped out for that process.
    507 	 */
    508 	microtime(&tv);
    509 	TIMEVAL_TO_TIMESPEC(&tv, &vap->va_ctime);
    510 	vap->va_atime = vap->va_mtime = vap->va_ctime;
    511 
    512 	switch (pfs->pfs_type) {
    513 	case Pmem:
    514 	case Pregs:
    515 	case Pfpregs:
    516 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    517 	PROCFS_MACHDEP_PROTECT_CASES
    518 #endif
    519 		/*
    520 		 * If the process has exercised some setuid or setgid
    521 		 * privilege, then rip away read/write permission so
    522 		 * that only root can gain access.
    523 		 */
    524 		if (procp->p_flag & P_SUGID)
    525 			vap->va_mode &= ~(S_IRUSR|S_IWUSR);
    526 		/* FALLTHROUGH */
    527 	case Pctl:
    528 	case Pstatus:
    529 	case Pnote:
    530 	case Pnotepg:
    531 	case Pmap:
    532 	case Pmaps:
    533 	case Pcmdline:
    534 		vap->va_nlink = 1;
    535 		vap->va_uid = procp->p_ucred->cr_uid;
    536 		vap->va_gid = procp->p_ucred->cr_gid;
    537 		break;
    538 	case Pmeminfo:
    539 	case Pcpuinfo:
    540 		vap->va_nlink = 1;
    541 		vap->va_uid = vap->va_gid = 0;
    542 		break;
    543 
    544 	default:
    545 		break;
    546 	}
    547 
    548 	/*
    549 	 * now do the object specific fields
    550 	 *
    551 	 * The size could be set from struct reg, but it's hardly
    552 	 * worth the trouble, and it puts some (potentially) machine
    553 	 * dependent data into this machine-independent code.  If it
    554 	 * becomes important then this function should break out into
    555 	 * a per-file stat function in the corresponding .c file.
    556 	 */
    557 
    558 	switch (pfs->pfs_type) {
    559 	case Proot:
    560 		/*
    561 		 * Set nlink to 1 to tell fts(3) we don't actually know.
    562 		 */
    563 		vap->va_nlink = 1;
    564 		vap->va_uid = 0;
    565 		vap->va_gid = 0;
    566 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    567 		break;
    568 
    569 	case Pcurproc: {
    570 		char buf[16];		/* should be enough */
    571 		vap->va_nlink = 1;
    572 		vap->va_uid = 0;
    573 		vap->va_gid = 0;
    574 		vap->va_bytes = vap->va_size =
    575 		    sprintf(buf, "%ld", (long)curproc->p_pid);
    576 		break;
    577 	}
    578 
    579 	case Pself:
    580 		vap->va_nlink = 1;
    581 		vap->va_uid = 0;
    582 		vap->va_gid = 0;
    583 		vap->va_bytes = vap->va_size = sizeof("curproc");
    584 		break;
    585 
    586 	case Pproc:
    587 		vap->va_nlink = 2;
    588 		vap->va_uid = procp->p_ucred->cr_uid;
    589 		vap->va_gid = procp->p_ucred->cr_gid;
    590 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    591 		break;
    592 
    593 	case Pfile:
    594 		error = EOPNOTSUPP;
    595 		break;
    596 
    597 	case Pmem:
    598 		vap->va_bytes = vap->va_size =
    599 			ctob(procp->p_vmspace->vm_tsize +
    600 				    procp->p_vmspace->vm_dsize +
    601 				    procp->p_vmspace->vm_ssize);
    602 		break;
    603 
    604 #if defined(PT_GETREGS) || defined(PT_SETREGS)
    605 	case Pregs:
    606 		vap->va_bytes = vap->va_size = sizeof(struct reg);
    607 		break;
    608 #endif
    609 
    610 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
    611 	case Pfpregs:
    612 		vap->va_bytes = vap->va_size = sizeof(struct fpreg);
    613 		break;
    614 #endif
    615 
    616 	case Pctl:
    617 	case Pstatus:
    618 	case Pnote:
    619 	case Pnotepg:
    620 	case Pcmdline:
    621 	case Pmeminfo:
    622 	case Pcpuinfo:
    623 		vap->va_bytes = vap->va_size = 0;
    624 		break;
    625 	case Pmap:
    626 	case Pmaps:
    627 		/*
    628 		 * Advise a larger blocksize for the map files, so that
    629 		 * they may be read in one pass.
    630 		 */
    631 		vap->va_blocksize = 4 * PAGE_SIZE;
    632 		vap->va_bytes = vap->va_size = 0;
    633 		break;
    634 
    635 #ifdef __HAVE_PROCFS_MACHDEP
    636 	PROCFS_MACHDEP_NODETYPE_CASES
    637 		error = procfs_machdep_getattr(ap->a_vp, vap, procp);
    638 		break;
    639 #endif
    640 
    641 	default:
    642 		panic("procfs_getattr");
    643 	}
    644 
    645 	return (error);
    646 }
    647 
    648 /*ARGSUSED*/
    649 int
    650 procfs_setattr(v)
    651 	void *v;
    652 {
    653 	/*
    654 	 * just fake out attribute setting
    655 	 * it's not good to generate an error
    656 	 * return, otherwise things like creat()
    657 	 * will fail when they try to set the
    658 	 * file length to 0.  worse, this means
    659 	 * that echo $note > /proc/$pid/note will fail.
    660 	 */
    661 
    662 	return (0);
    663 }
    664 
    665 /*
    666  * implement access checking.
    667  *
    668  * actually, the check for super-user is slightly
    669  * broken since it will allow read access to write-only
    670  * objects.  this doesn't cause any particular trouble
    671  * but does mean that the i/o entry points need to check
    672  * that the operation really does make sense.
    673  */
    674 int
    675 procfs_access(v)
    676 	void *v;
    677 {
    678 	struct vop_access_args /* {
    679 		struct vnode *a_vp;
    680 		int a_mode;
    681 		struct ucred *a_cred;
    682 		struct proc *a_p;
    683 	} */ *ap = v;
    684 	struct vattr va;
    685 	int error;
    686 
    687 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
    688 		return (error);
    689 
    690 	return (vaccess(va.va_type, va.va_mode,
    691 	    va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
    692 }
    693 
    694 /*
    695  * lookup.  this is incredibly complicated in the
    696  * general case, however for most pseudo-filesystems
    697  * very little needs to be done.
    698  *
    699  * Locking isn't hard here, just poorly documented.
    700  *
    701  * If we're looking up ".", just vref the parent & return it.
    702  *
    703  * If we're looking up "..", unlock the parent, and lock "..". If everything
    704  * went ok, and we're on the last component and the caller requested the
    705  * parent locked, try to re-lock the parent. We do this to prevent lock
    706  * races.
    707  *
    708  * For anything else, get the needed node. Then unlock the parent if not
    709  * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
    710  * parent in the .. case).
    711  *
    712  * We try to exit with the parent locked in error cases.
    713  */
    714 int
    715 procfs_lookup(v)
    716 	void *v;
    717 {
    718 	struct vop_lookup_args /* {
    719 		struct vnode * a_dvp;
    720 		struct vnode ** a_vpp;
    721 		struct componentname * a_cnp;
    722 	} */ *ap = v;
    723 	struct componentname *cnp = ap->a_cnp;
    724 	struct vnode **vpp = ap->a_vpp;
    725 	struct vnode *dvp = ap->a_dvp;
    726 	const char *pname = cnp->cn_nameptr;
    727 	const struct proc_target *pt = NULL;
    728 	struct vnode *fvp;
    729 	pid_t pid;
    730 	struct pfsnode *pfs;
    731 	struct proc *p = NULL;
    732 	int i, error, wantpunlock, iscurproc = 0, isself = 0;
    733 
    734 	*vpp = NULL;
    735 	cnp->cn_flags &= ~PDIRUNLOCK;
    736 
    737 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    738 		return (EROFS);
    739 
    740 	if (cnp->cn_namelen == 1 && *pname == '.') {
    741 		*vpp = dvp;
    742 		VREF(dvp);
    743 		return (0);
    744 	}
    745 
    746 	wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
    747 	pfs = VTOPFS(dvp);
    748 	switch (pfs->pfs_type) {
    749 	case Proot:
    750 		/*
    751 		 * Shouldn't get here with .. in the root node.
    752 		 */
    753 		if (cnp->cn_flags & ISDOTDOT)
    754 			return (EIO);
    755 
    756 		iscurproc = CNEQ(cnp, "curproc", 7);
    757 		isself = CNEQ(cnp, "self", 4);
    758 
    759 		if (iscurproc || isself) {
    760 			error = procfs_allocvp(dvp->v_mount, vpp, 0,
    761 			    iscurproc ? Pcurproc : Pself);
    762 			if ((error == 0) && (wantpunlock)) {
    763 				VOP_UNLOCK(dvp, 0);
    764 				cnp->cn_flags |= PDIRUNLOCK;
    765 			}
    766 			return (error);
    767 		}
    768 
    769 		for (i = 0; i < nproc_root_targets; i++) {
    770 			pt = &proc_root_targets[i];
    771 			if (cnp->cn_namelen == pt->pt_namlen &&
    772 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
    773 			    (pt->pt_valid == NULL ||
    774 			     (*pt->pt_valid)(p, dvp->v_mount)))
    775 				break;
    776 		}
    777 
    778 		if (i != nproc_root_targets) {
    779 			error = procfs_allocvp(dvp->v_mount, vpp, 0,
    780 			    pt->pt_pfstype);
    781 			if ((error == 0) && (wantpunlock)) {
    782 				VOP_UNLOCK(dvp, 0);
    783 				cnp->cn_flags |= PDIRUNLOCK;
    784 			}
    785 			return (error);
    786 		}
    787 
    788 		pid = atopid(pname, cnp->cn_namelen);
    789 		if (pid == NO_PID)
    790 			break;
    791 
    792 		p = PFIND(pid);
    793 		if (p == 0)
    794 			break;
    795 
    796 		error = procfs_allocvp(dvp->v_mount, vpp, pid, Pproc);
    797 		if ((error == 0) && (wantpunlock)) {
    798 			VOP_UNLOCK(dvp, 0);
    799 			cnp->cn_flags |= PDIRUNLOCK;
    800 		}
    801 		return (error);
    802 
    803 	case Pproc:
    804 		/*
    805 		 * do the .. dance. We unlock the directory, and then
    806 		 * get the root dir. That will automatically return ..
    807 		 * locked. Then if the caller wanted dvp locked, we
    808 		 * re-lock.
    809 		 */
    810 		if (cnp->cn_flags & ISDOTDOT) {
    811 			VOP_UNLOCK(dvp, 0);
    812 			cnp->cn_flags |= PDIRUNLOCK;
    813 			error = procfs_root(dvp->v_mount, vpp);
    814 			if ((error == 0) && (wantpunlock == 0) &&
    815 				    ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
    816 				cnp->cn_flags &= ~PDIRUNLOCK;
    817 			return (error);
    818 		}
    819 
    820 		p = PFIND(pfs->pfs_pid);
    821 		if (p == 0)
    822 			break;
    823 
    824 		for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
    825 			if (cnp->cn_namelen == pt->pt_namlen &&
    826 			    memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
    827 			    (pt->pt_valid == NULL ||
    828 			     (*pt->pt_valid)(p, dvp->v_mount)))
    829 				goto found;
    830 		}
    831 		break;
    832 
    833 	found:
    834 		if (pt->pt_pfstype == Pfile) {
    835 			fvp = p->p_textvp;
    836 			/* We already checked that it exists. */
    837 			VREF(fvp);
    838 			vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
    839 			if (wantpunlock) {
    840 				VOP_UNLOCK(dvp, 0);
    841 				cnp->cn_flags |= PDIRUNLOCK;
    842 			}
    843 			*vpp = fvp;
    844 			return (0);
    845 		}
    846 
    847 		error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
    848 					pt->pt_pfstype);
    849 		if ((error == 0) && (wantpunlock)) {
    850 			VOP_UNLOCK(dvp, 0);
    851 			cnp->cn_flags |= PDIRUNLOCK;
    852 		}
    853 		return (error);
    854 
    855 	default:
    856 		return (ENOTDIR);
    857 	}
    858 
    859 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
    860 }
    861 
    862 int
    863 procfs_validfile(p, mp)
    864 	struct proc *p;
    865 	struct mount *mp;
    866 {
    867 	return (p->p_textvp != NULL);
    868 }
    869 
    870 static int
    871 procfs_validfile_linux(p, mp)
    872 	struct proc *p;
    873 	struct mount *mp;
    874 {
    875 	int flags;
    876 
    877 	flags = VFSTOPROC(mp)->pmnt_flags;
    878 	return ((flags & PROCFSMNT_LINUXCOMPAT) &&
    879 	    (p == NULL || procfs_validfile(p, mp)));
    880 }
    881 
    882 /*
    883  * readdir returns directory entries from pfsnode (vp).
    884  *
    885  * the strategy here with procfs is to generate a single
    886  * directory entry at a time (struct dirent) and then
    887  * copy that out to userland using uiomove.  a more efficent
    888  * though more complex implementation, would try to minimize
    889  * the number of calls to uiomove().  for procfs, this is
    890  * hardly worth the added code complexity.
    891  *
    892  * this should just be done through read()
    893  */
    894 int
    895 procfs_readdir(v)
    896 	void *v;
    897 {
    898 	struct vop_readdir_args /* {
    899 		struct vnode *a_vp;
    900 		struct uio *a_uio;
    901 		struct ucred *a_cred;
    902 		int *a_eofflag;
    903 		off_t **a_cookies;
    904 		int *a_ncookies;
    905 	} */ *ap = v;
    906 	struct uio *uio = ap->a_uio;
    907 	struct dirent d;
    908 	struct pfsnode *pfs;
    909 	off_t i;
    910 	int error;
    911 	off_t *cookies = NULL;
    912 	int ncookies, left, skip, j;
    913 	struct vnode *vp;
    914 	const struct proc_target *pt;
    915 
    916 	vp = ap->a_vp;
    917 	pfs = VTOPFS(vp);
    918 
    919 	if (uio->uio_resid < UIO_MX)
    920 		return (EINVAL);
    921 	if (uio->uio_offset < 0)
    922 		return (EINVAL);
    923 
    924 	error = 0;
    925 	i = uio->uio_offset;
    926 	memset((caddr_t)&d, 0, UIO_MX);
    927 	d.d_reclen = UIO_MX;
    928 	ncookies = uio->uio_resid / UIO_MX;
    929 
    930 	switch (pfs->pfs_type) {
    931 	/*
    932 	 * this is for the process-specific sub-directories.
    933 	 * all that is needed to is copy out all the entries
    934 	 * from the procent[] table (top of this file).
    935 	 */
    936 	case Pproc: {
    937 		struct proc *p;
    938 
    939 		if (i >= nproc_targets)
    940 			return 0;
    941 
    942 		p = PFIND(pfs->pfs_pid);
    943 		if (p == NULL)
    944 			break;
    945 
    946 		if (ap->a_ncookies) {
    947 			ncookies = min(ncookies, (nproc_targets - i));
    948 			cookies = malloc(ncookies * sizeof (off_t),
    949 			    M_TEMP, M_WAITOK);
    950 			*ap->a_cookies = cookies;
    951 		}
    952 
    953 		for (pt = &proc_targets[i];
    954 		     uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
    955 			if (pt->pt_valid &&
    956 			    (*pt->pt_valid)(p, vp->v_mount) == 0)
    957 				continue;
    958 
    959 			d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype);
    960 			d.d_namlen = pt->pt_namlen;
    961 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
    962 			d.d_type = pt->pt_type;
    963 
    964 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    965 				break;
    966 			if (cookies)
    967 				*cookies++ = i + 1;
    968 		}
    969 
    970 	    	break;
    971 	}
    972 
    973 	/*
    974 	 * this is for the root of the procfs filesystem
    975 	 * what is needed are special entries for "curproc"
    976 	 * and "self" followed by an entry for each process
    977 	 * on allproc
    978 #ifdef PROCFS_ZOMBIE
    979 	 * and deadproc and zombproc.
    980 #endif
    981 	 */
    982 
    983 	case Proot: {
    984 		int pcnt = i, nc = 0;
    985 		const struct proclist_desc *pd;
    986 		volatile struct proc *p;
    987 
    988 		if (pcnt > 3)
    989 			pcnt = 3;
    990 		if (ap->a_ncookies) {
    991 			/*
    992 			 * XXX Potentially allocating too much space here,
    993 			 * but I'm lazy. This loop needs some work.
    994 			 */
    995 			cookies = malloc(ncookies * sizeof (off_t),
    996 			    M_TEMP, M_WAITOK);
    997 			*ap->a_cookies = cookies;
    998 		}
    999 		/*
   1000 		 * XXX: THIS LOOP ASSUMES THAT allproc IS THE FIRST
   1001 		 * PROCLIST IN THE proclists!
   1002 		 */
   1003 		proclist_lock_read();
   1004 		pd = proclists;
   1005 #ifdef PROCFS_ZOMBIE
   1006 	again:
   1007 #endif
   1008 		for (p = LIST_FIRST(pd->pd_list);
   1009 		     p != NULL && uio->uio_resid >= UIO_MX; i++, pcnt++) {
   1010 			switch (i) {
   1011 			case 0:		/* `.' */
   1012 			case 1:		/* `..' */
   1013 				d.d_fileno = PROCFS_FILENO(0, Proot);
   1014 				d.d_namlen = i + 1;
   1015 				memcpy(d.d_name, "..", d.d_namlen);
   1016 				d.d_name[i + 1] = '\0';
   1017 				d.d_type = DT_DIR;
   1018 				break;
   1019 
   1020 			case 2:
   1021 				d.d_fileno = PROCFS_FILENO(0, Pcurproc);
   1022 				d.d_namlen = sizeof("curproc") - 1;
   1023 				memcpy(d.d_name, "curproc", sizeof("curproc"));
   1024 				d.d_type = DT_LNK;
   1025 				break;
   1026 
   1027 			case 3:
   1028 				d.d_fileno = PROCFS_FILENO(0, Pself);
   1029 				d.d_namlen = sizeof("self") - 1;
   1030 				memcpy(d.d_name, "self", sizeof("self"));
   1031 				d.d_type = DT_LNK;
   1032 				break;
   1033 
   1034 			default:
   1035 				while (pcnt < i) {
   1036 					pcnt++;
   1037 					p = LIST_NEXT(p, p_list);
   1038 					if (!p)
   1039 						goto done;
   1040 				}
   1041 				d.d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
   1042 				d.d_namlen = sprintf(d.d_name, "%ld",
   1043 				    (long)p->p_pid);
   1044 				d.d_type = DT_DIR;
   1045 				p = p->p_list.le_next;
   1046 				break;
   1047 			}
   1048 
   1049 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1050 				break;
   1051 			nc++;
   1052 			if (cookies)
   1053 				*cookies++ = i + 1;
   1054 		}
   1055 	done:
   1056 
   1057 #ifdef PROCFS_ZOMBIE
   1058 		pd++;
   1059 		if (p == NULL && pd->pd_list != NULL)
   1060 			goto again;
   1061 #endif
   1062 		proclist_unlock_read();
   1063 
   1064 		skip = i - pcnt;
   1065 		if (skip >= nproc_root_targets)
   1066 			break;
   1067 		left = nproc_root_targets - skip;
   1068 		for (j = 0, pt = &proc_root_targets[0];
   1069 		     uio->uio_resid >= UIO_MX && j < left;
   1070 		     pt++, j++, i++) {
   1071 			if (pt->pt_valid &&
   1072 			    (*pt->pt_valid)(NULL, vp->v_mount) == 0)
   1073 				continue;
   1074 			d.d_fileno = PROCFS_FILENO(0, pt->pt_pfstype);
   1075 			d.d_namlen = pt->pt_namlen;
   1076 			memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
   1077 			d.d_type = pt->pt_type;
   1078 
   1079 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1080 				break;
   1081 			nc++;
   1082 			if (cookies)
   1083 				*cookies++ = i + 1;
   1084 		}
   1085 
   1086 		ncookies = nc;
   1087 		break;
   1088 	}
   1089 
   1090 	default:
   1091 		error = ENOTDIR;
   1092 		break;
   1093 	}
   1094 
   1095 	if (ap->a_ncookies) {
   1096 		if (error) {
   1097 			if (cookies)
   1098 				free(*ap->a_cookies, M_TEMP);
   1099 			*ap->a_ncookies = 0;
   1100 			*ap->a_cookies = NULL;
   1101 		} else
   1102 			*ap->a_ncookies = ncookies;
   1103 	}
   1104 	uio->uio_offset = i;
   1105 	return (error);
   1106 }
   1107 
   1108 /*
   1109  * readlink reads the link of `curproc'
   1110  */
   1111 int
   1112 procfs_readlink(v)
   1113 	void *v;
   1114 {
   1115 	struct vop_readlink_args *ap = v;
   1116 	char buf[16];		/* should be enough */
   1117 	int len;
   1118 
   1119 	if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pcurproc))
   1120 		len = sprintf(buf, "%ld", (long)curproc->p_pid);
   1121 	else if (VTOPFS(ap->a_vp)->pfs_fileno == PROCFS_FILENO(0, Pself))
   1122 		len = sprintf(buf, "%s", "curproc");
   1123 	else
   1124 		return (EINVAL);
   1125 
   1126 	return (uiomove((caddr_t)buf, len, ap->a_uio));
   1127 }
   1128 
   1129 /*
   1130  * convert decimal ascii to pid_t
   1131  */
   1132 static pid_t
   1133 atopid(b, len)
   1134 	const char *b;
   1135 	u_int len;
   1136 {
   1137 	pid_t p = 0;
   1138 
   1139 	while (len--) {
   1140 		char c = *b++;
   1141 		if (c < '0' || c > '9')
   1142 			return (NO_PID);
   1143 		p = 10 * p + (c - '0');
   1144 		if (p > PID_MAX)
   1145 			return (NO_PID);
   1146 	}
   1147 
   1148 	return (p);
   1149 }
   1150