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