Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.47
      1 /*	$NetBSD: procfs_subr.c,v 1.47 2003/03/04 18:55:02 tron Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Christopher G. Demetriou.  All rights reserved.
      5  * Copyright (c) 1993 Jan-Simon Pendry
      6  * Copyright (c) 1993
      7  *	The Regents of the University of California.  All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * Jan-Simon Pendry.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.47 2003/03/04 18:55:02 tron Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/time.h>
     49 #include <sys/kernel.h>
     50 #include <sys/proc.h>
     51 #include <sys/vnode.h>
     52 #include <sys/malloc.h>
     53 #include <sys/stat.h>
     54 #include <sys/file.h>
     55 #include <sys/filedesc.h>
     56 
     57 #include <miscfs/procfs/procfs.h>
     58 
     59 void procfs_hashins __P((struct pfsnode *));
     60 void procfs_hashrem __P((struct pfsnode *));
     61 struct vnode *procfs_hashget __P((pid_t, pfstype, int, struct mount *));
     62 
     63 LIST_HEAD(pfs_hashhead, pfsnode) *pfs_hashtbl;
     64 u_long	pfs_ihash;	/* size of hash table - 1 */
     65 #define PFSPIDHASH(pid)	((pid) & pfs_ihash)
     66 
     67 struct lock pfs_hashlock;
     68 struct simplelock pfs_hash_slock;
     69 
     70 #define	ISSET(t, f)	((t) & (f))
     71 
     72 /*
     73  * allocate a pfsnode/vnode pair.  the vnode is
     74  * referenced, and locked.
     75  *
     76  * the pid, pfs_type, and mount point uniquely
     77  * identify a pfsnode.  the mount point is needed
     78  * because someone might mount this filesystem
     79  * twice.
     80  *
     81  * all pfsnodes are maintained on a singly-linked
     82  * list.  new nodes are only allocated when they cannot
     83  * be found on this list.  entries on the list are
     84  * removed when the vfs reclaim entry is called.
     85  *
     86  * a single lock is kept for the entire list.  this is
     87  * needed because the getnewvnode() function can block
     88  * waiting for a vnode to become free, in which case there
     89  * may be more than one process trying to get the same
     90  * vnode.  this lock is only taken if we are going to
     91  * call getnewvnode, since the kernel itself is single-threaded.
     92  *
     93  * if an entry is found on the list, then call vget() to
     94  * take a reference.  this is done because there may be
     95  * zero references to it and so it needs to removed from
     96  * the vnode free list.
     97  */
     98 int
     99 procfs_allocvp(mp, vpp, pid, pfs_type, fd)
    100 	struct mount *mp;
    101 	struct vnode **vpp;
    102 	pid_t pid;
    103 	pfstype pfs_type;
    104 	int fd;
    105 {
    106 	struct pfsnode *pfs;
    107 	struct vnode *vp;
    108 	int error;
    109 
    110 	do {
    111 		if ((*vpp = procfs_hashget(pid, pfs_type, fd, mp)) != NULL)
    112 			return (0);
    113 	} while (lockmgr(&pfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
    114 
    115 	if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, &vp)) != 0) {
    116 		*vpp = NULL;
    117 		lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    118 		return (error);
    119 	}
    120 
    121 	MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
    122 	vp->v_data = pfs;
    123 
    124 	pfs->pfs_pid = pid;
    125 	pfs->pfs_type = pfs_type;
    126 	pfs->pfs_vnode = vp;
    127 	pfs->pfs_flags = 0;
    128 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type, fd);
    129 	pfs->pfs_fd = fd;
    130 
    131 	switch (pfs_type) {
    132 	case Proot:	/* /proc = dr-xr-xr-x */
    133 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    134 		vp->v_type = VDIR;
    135 		vp->v_flag = VROOT;
    136 		break;
    137 
    138 	case Pcurproc:	/* /proc/curproc = lr-xr-xr-x */
    139 	case Pself:	/* /proc/self    = lr-xr-xr-x */
    140 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    141 		vp->v_type = VLNK;
    142 		break;
    143 
    144 	case Pproc:	/* /proc/N = dr-xr-xr-x */
    145 	case Pfd:
    146 		if (fd == -1) {	/* /proc/N/fd = dr-xr-xr-x */
    147 			pfs->pfs_mode =
    148 			    S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    149 			vp->v_type = VDIR;
    150 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
    151 			struct file *fp;
    152 			struct vnode *vxp;
    153 
    154 			/* XXX can procfs_getfp() ever fail here? */
    155 			if ((error = procfs_getfp(pfs, &fp)) != 0)
    156 				goto bad;
    157 
    158 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
    159 			switch (fp->f_type) {
    160 			case DTYPE_VNODE:
    161 				vxp = (struct vnode *)fp->f_data;
    162 				vp->v_type = vxp->v_type;
    163 				break;
    164 			case DTYPE_PIPE:
    165 				vp->v_type = VFIFO;
    166 				break;
    167 			case DTYPE_SOCKET:
    168 				vp->v_type = VSOCK;
    169 				break;
    170 			default:
    171 				error = EOPNOTSUPP;
    172 				goto bad;
    173 			}
    174 		}
    175 		break;
    176 
    177 	case Pfile:	/* /proc/N/file = -rw------- */
    178 	case Pmem:	/* /proc/N/mem = -rw------- */
    179 	case Pregs:	/* /proc/N/regs = -rw------- */
    180 	case Pfpregs:	/* /proc/N/fpregs = -rw------- */
    181 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    182 		vp->v_type = VREG;
    183 		break;
    184 
    185 	case Pctl:	/* /proc/N/ctl = --w------ */
    186 	case Pnote:	/* /proc/N/note = --w------ */
    187 	case Pnotepg:	/* /proc/N/notepg = --w------ */
    188 		pfs->pfs_mode = S_IWUSR;
    189 		vp->v_type = VREG;
    190 		break;
    191 
    192 	case Pmap:	/* /proc/N/map = -r--r--r-- */
    193 	case Pmaps:	/* /proc/N/maps = -r--r--r-- */
    194 	case Pstatus:	/* /proc/N/status = -r--r--r-- */
    195 	case Pcmdline:	/* /proc/N/cmdline = -r--r--r-- */
    196 	case Pmeminfo:	/* /proc/meminfo = -r--r--r-- */
    197 	case Pcpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
    198 	case Puptime:	/* /proc/uptime = -r--r--r-- */
    199 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    200 		vp->v_type = VREG;
    201 		break;
    202 
    203 #ifdef __HAVE_PROCFS_MACHDEP
    204 	PROCFS_MACHDEP_NODETYPE_CASES
    205 		procfs_machdep_allocvp(vp);
    206 		break;
    207 #endif
    208 
    209 	default:
    210 		panic("procfs_allocvp");
    211 	}
    212 
    213 	procfs_hashins(pfs);
    214 	uvm_vnp_setsize(vp, 0);
    215 	lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    216 
    217 	*vpp = vp;
    218 	return (0);
    219 
    220  bad:
    221 	FREE(pfs, M_TEMP);
    222 	ungetnewvnode(vp);
    223 	return (error);
    224 }
    225 
    226 int
    227 procfs_freevp(vp)
    228 	struct vnode *vp;
    229 {
    230 	struct pfsnode *pfs = VTOPFS(vp);
    231 
    232 	procfs_hashrem(pfs);
    233 
    234 	FREE(vp->v_data, M_TEMP);
    235 	vp->v_data = 0;
    236 	return (0);
    237 }
    238 
    239 int
    240 procfs_rw(v)
    241 	void *v;
    242 {
    243 	struct vop_read_args *ap = v;
    244 	struct vnode *vp = ap->a_vp;
    245 	struct uio *uio = ap->a_uio;
    246 	struct proc *curp = uio->uio_procp;
    247 	struct pfsnode *pfs = VTOPFS(vp);
    248 	struct lwp *l;
    249 	struct proc *p;
    250 
    251 	p = PFIND(pfs->pfs_pid);
    252 	if (p == 0)
    253 		return (EINVAL);
    254 
    255 	/* XXX NJWLWP
    256 	 * The entire procfs interface needs work to be useful to
    257 	 * a process with multiple LWPs. For the moment, we'll
    258 	 * just kluge this and fail on others.
    259 	 */
    260 	l = proc_representative_lwp(p);
    261 
    262 	switch (pfs->pfs_type) {
    263 	case Pregs:
    264 	case Pfpregs:
    265 	case Pmem:
    266 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    267 	PROCFS_MACHDEP_PROTECT_CASES
    268 #endif
    269 		/*
    270 		 * Do not allow init to be modified while in secure mode; it
    271 		 * could be duped into changing the security level.
    272 		 */
    273 		if (uio->uio_rw == UIO_WRITE &&
    274 		    p == initproc && securelevel > -1)
    275 			return (EPERM);
    276 		break;
    277 
    278 	default:
    279 		break;
    280 	}
    281 
    282 	switch (pfs->pfs_type) {
    283 	case Pnote:
    284 	case Pnotepg:
    285 		return (procfs_donote(curp, p, pfs, uio));
    286 
    287 	case Pregs:
    288 		return (procfs_doregs(curp, l, pfs, uio));
    289 
    290 	case Pfpregs:
    291 		return (procfs_dofpregs(curp, l, pfs, uio));
    292 
    293 	case Pctl:
    294 		return (procfs_doctl(curp, l, pfs, uio));
    295 
    296 	case Pstatus:
    297 		return (procfs_dostatus(curp, l, pfs, uio));
    298 
    299 	case Pmap:
    300 		return (procfs_domap(curp, p, pfs, uio, 0));
    301 
    302 	case Pmaps:
    303 		return (procfs_domap(curp, p, pfs, uio, 1));
    304 
    305 	case Pmem:
    306 		return (procfs_domem(curp, p, pfs, uio));
    307 
    308 	case Pcmdline:
    309 		return (procfs_docmdline(curp, p, pfs, uio));
    310 
    311 	case Pmeminfo:
    312 		return (procfs_domeminfo(curp, p, pfs, uio));
    313 
    314 	case Pcpuinfo:
    315 		return (procfs_docpuinfo(curp, p, pfs, uio));
    316 
    317 	case Pfd:
    318 		return (procfs_dofd(curp, p, pfs, uio));
    319 
    320 	case Puptime:
    321 		return (procfs_douptime(curp, p, pfs, uio));
    322 
    323 #ifdef __HAVE_PROCFS_MACHDEP
    324 	PROCFS_MACHDEP_NODETYPE_CASES
    325 		return (procfs_machdep_rw(curp, l, pfs, uio));
    326 #endif
    327 
    328 	default:
    329 		return (EOPNOTSUPP);
    330 	}
    331 }
    332 
    333 /*
    334  * Get a string from userland into (buf).  Strip a trailing
    335  * nl character (to allow easy access from the shell).
    336  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    337  * will automatically add a nul char at the end.
    338  *
    339  * Returns 0 on success or the following errors
    340  *
    341  * EINVAL:    file offset is non-zero.
    342  * EMSGSIZE:  message is longer than kernel buffer
    343  * EFAULT:    user i/o buffer is not addressable
    344  */
    345 int
    346 vfs_getuserstr(uio, buf, buflenp)
    347 	struct uio *uio;
    348 	char *buf;
    349 	int *buflenp;
    350 {
    351 	int xlen;
    352 	int error;
    353 
    354 	if (uio->uio_offset != 0)
    355 		return (EINVAL);
    356 
    357 	xlen = *buflenp;
    358 
    359 	/* must be able to read the whole string in one go */
    360 	if (xlen < uio->uio_resid)
    361 		return (EMSGSIZE);
    362 	xlen = uio->uio_resid;
    363 
    364 	if ((error = uiomove(buf, xlen, uio)) != 0)
    365 		return (error);
    366 
    367 	/* allow multiple writes without seeks */
    368 	uio->uio_offset = 0;
    369 
    370 	/* cleanup string and remove trailing newline */
    371 	buf[xlen] = '\0';
    372 	xlen = strlen(buf);
    373 	if (xlen > 0 && buf[xlen-1] == '\n')
    374 		buf[--xlen] = '\0';
    375 	*buflenp = xlen;
    376 
    377 	return (0);
    378 }
    379 
    380 const vfs_namemap_t *
    381 vfs_findname(nm, buf, buflen)
    382 	const vfs_namemap_t *nm;
    383 	const char *buf;
    384 	int buflen;
    385 {
    386 
    387 	for (; nm->nm_name; nm++)
    388 		if (memcmp(buf, nm->nm_name, buflen+1) == 0)
    389 			return (nm);
    390 
    391 	return (0);
    392 }
    393 
    394 /*
    395  * Initialize pfsnode hash table.
    396  */
    397 void
    398 procfs_hashinit()
    399 {
    400 	lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
    401 	pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
    402 	    M_WAITOK, &pfs_ihash);
    403 	simple_lock_init(&pfs_hash_slock);
    404 }
    405 
    406 void
    407 procfs_hashreinit()
    408 {
    409 	struct pfsnode *pp;
    410 	struct pfs_hashhead *oldhash, *hash;
    411 	u_long i, oldmask, mask, val;
    412 
    413 	hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
    414 	    &mask);
    415 
    416 	simple_lock(&pfs_hash_slock);
    417 	oldhash = pfs_hashtbl;
    418 	oldmask = pfs_ihash;
    419 	pfs_hashtbl = hash;
    420 	pfs_ihash = mask;
    421 	for (i = 0; i <= oldmask; i++) {
    422 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    423 			LIST_REMOVE(pp, pfs_hash);
    424 			val = PFSPIDHASH(pp->pfs_pid);
    425 			LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
    426 		}
    427 	}
    428 	simple_unlock(&pfs_hash_slock);
    429 	hashdone(oldhash, M_UFSMNT);
    430 }
    431 
    432 /*
    433  * Free pfsnode hash table.
    434  */
    435 void
    436 procfs_hashdone()
    437 {
    438 	hashdone(pfs_hashtbl, M_UFSMNT);
    439 }
    440 
    441 struct vnode *
    442 procfs_hashget(pid, type, fd, mp)
    443 	pid_t pid;
    444 	pfstype type;
    445 	int fd;
    446 	struct mount *mp;
    447 {
    448 	struct pfs_hashhead *ppp;
    449 	struct pfsnode *pp;
    450 	struct vnode *vp;
    451 
    452 loop:
    453 	simple_lock(&pfs_hash_slock);
    454 	ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
    455 	LIST_FOREACH(pp, ppp, pfs_hash) {
    456 		vp = PFSTOV(pp);
    457 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
    458 		    pp->pfs_fd == fd && vp->v_mount == mp) {
    459 			simple_lock(&vp->v_interlock);
    460 			simple_unlock(&pfs_hash_slock);
    461 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    462 				goto loop;
    463 			return (vp);
    464 		}
    465 	}
    466 	simple_unlock(&pfs_hash_slock);
    467 	return (NULL);
    468 }
    469 
    470 /*
    471  * Insert the pfsnode into the hash table and lock it.
    472  */
    473 void
    474 procfs_hashins(pp)
    475 	struct pfsnode *pp;
    476 {
    477 	struct pfs_hashhead *ppp;
    478 
    479 	/* lock the pfsnode, then put it on the appropriate hash list */
    480 	lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
    481 
    482 	simple_lock(&pfs_hash_slock);
    483 	ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
    484 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
    485 	simple_unlock(&pfs_hash_slock);
    486 }
    487 
    488 /*
    489  * Remove the pfsnode from the hash table.
    490  */
    491 void
    492 procfs_hashrem(pp)
    493 	struct pfsnode *pp;
    494 {
    495 	simple_lock(&pfs_hash_slock);
    496 	LIST_REMOVE(pp, pfs_hash);
    497 	simple_unlock(&pfs_hash_slock);
    498 }
    499 
    500 void
    501 procfs_revoke_vnodes(p, arg)
    502 	struct proc *p;
    503 	void *arg;
    504 {
    505 	struct pfsnode *pfs, *pnext;
    506 	struct vnode *vp;
    507 	struct mount *mp = (struct mount *)arg;
    508 	struct pfs_hashhead *ppp;
    509 
    510 	if (!(p->p_flag & P_SUGID))
    511 		return;
    512 
    513 	ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
    514 	for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
    515 		vp = PFSTOV(pfs);
    516 		pnext = LIST_NEXT(pfs, pfs_hash);
    517 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
    518 		    vp->v_mount == mp)
    519 			VOP_REVOKE(vp, REVOKEALL);
    520 	}
    521 }
    522 
    523 int
    524 procfs_getfp(pfs, fp)
    525 	struct pfsnode *pfs;
    526 	struct file **fp;
    527 {
    528 	struct proc *p = PFIND(pfs->pfs_pid);
    529 
    530 	if (p == NULL)
    531 		return ESRCH;
    532 
    533 	if (pfs->pfs_fd == -1)
    534 		return EINVAL;
    535 
    536 	if ((*fp = p->p_fd->fd_ofiles[pfs->pfs_fd]) == NULL)
    537 		return EBADF;
    538 	return 0;
    539 }
    540