Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.42
      1 /*	$NetBSD: procfs_subr.c,v 1.42 2003/01/03 13:21:18 christos 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.42 2003/01/03 13:21:18 christos 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, vpp)) != 0) {
    116 		*vpp = NULL;
    117 		lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    118 		return (error);
    119 	}
    120 	vp = *vpp;
    121 
    122 	MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
    123 	vp->v_data = pfs;
    124 
    125 	pfs->pfs_pid = pid;
    126 	pfs->pfs_type = pfs_type;
    127 	pfs->pfs_vnode = vp;
    128 	pfs->pfs_flags = 0;
    129 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type, fd);
    130 	pfs->pfs_fd = fd;
    131 
    132 	switch (pfs_type) {
    133 	case Proot:	/* /proc = dr-xr-xr-x */
    134 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    135 		vp->v_type = VDIR;
    136 		vp->v_flag = VROOT;
    137 		break;
    138 
    139 	case Pcurproc:	/* /proc/curproc = lr-xr-xr-x */
    140 	case Pself:	/* /proc/self    = lr-xr-xr-x */
    141 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    142 		vp->v_type = VLNK;
    143 		break;
    144 
    145 	case Pproc:	/* /proc/N = dr-xr-xr-x */
    146 	case Pfd:
    147 		if (fd == -1) {	/* /proc/N/fd = dr-xr-xr-x */
    148 			pfs->pfs_mode =
    149 			    S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    150 			vp->v_type = VDIR;
    151 		} else {	/* /proc/N/fd/M = [ps-]rw------- */
    152 			struct file *fp;
    153 			struct vnode *vxp;
    154 			if ((error = procfs_getfp(pfs, &fp)) != 0)
    155 				return error;
    156 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
    157 			switch (fp->f_type) {
    158 			case DTYPE_VNODE:
    159 				vxp = (struct vnode *)fp->f_data;
    160 				vp->v_type = vxp->v_type;
    161 				break;
    162 			case DTYPE_PIPE:
    163 				vp->v_type = VFIFO;
    164 				break;
    165 			case DTYPE_SOCKET:
    166 				vp->v_type = VSOCK;
    167 				break;
    168 			case DTYPE_KQUEUE:
    169 			case DTYPE_MISC:
    170 				return EOPNOTSUPP;
    171 			default:
    172 				panic("unknown file type %d\n", fp->f_type);
    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 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    199 		vp->v_type = VREG;
    200 		break;
    201 
    202 #ifdef __HAVE_PROCFS_MACHDEP
    203 	PROCFS_MACHDEP_NODETYPE_CASES
    204 		procfs_machdep_allocvp(vp);
    205 		break;
    206 #endif
    207 
    208 	default:
    209 		panic("procfs_allocvp");
    210 	}
    211 
    212 	procfs_hashins(pfs);
    213 	uvm_vnp_setsize(vp, 0);
    214 	lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    215 
    216 	return (error);
    217 }
    218 
    219 int
    220 procfs_freevp(vp)
    221 	struct vnode *vp;
    222 {
    223 	struct pfsnode *pfs = VTOPFS(vp);
    224 
    225 	procfs_hashrem(pfs);
    226 
    227 	FREE(vp->v_data, M_TEMP);
    228 	vp->v_data = 0;
    229 	return (0);
    230 }
    231 
    232 int
    233 procfs_rw(v)
    234 	void *v;
    235 {
    236 	struct vop_read_args *ap = v;
    237 	struct vnode *vp = ap->a_vp;
    238 	struct uio *uio = ap->a_uio;
    239 	struct proc *curp = uio->uio_procp;
    240 	struct pfsnode *pfs = VTOPFS(vp);
    241 	struct proc *p;
    242 
    243 	p = PFIND(pfs->pfs_pid);
    244 	if (p == 0)
    245 		return (EINVAL);
    246 
    247 	switch (pfs->pfs_type) {
    248 	case Pregs:
    249 	case Pfpregs:
    250 	case Pmem:
    251 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    252 	PROCFS_MACHDEP_PROTECT_CASES
    253 #endif
    254 		/*
    255 		 * Do not allow init to be modified while in secure mode; it
    256 		 * could be duped into changing the security level.
    257 		 */
    258 		if (uio->uio_rw == UIO_WRITE &&
    259 		    p == initproc && securelevel > -1)
    260 			return (EPERM);
    261 		break;
    262 
    263 	default:
    264 		break;
    265 	}
    266 
    267 	switch (pfs->pfs_type) {
    268 	case Pnote:
    269 	case Pnotepg:
    270 		return (procfs_donote(curp, p, pfs, uio));
    271 
    272 	case Pregs:
    273 		return (procfs_doregs(curp, p, pfs, uio));
    274 
    275 	case Pfpregs:
    276 		return (procfs_dofpregs(curp, p, pfs, uio));
    277 
    278 	case Pctl:
    279 		return (procfs_doctl(curp, p, pfs, uio));
    280 
    281 	case Pstatus:
    282 		return (procfs_dostatus(curp, p, pfs, uio));
    283 
    284 	case Pmap:
    285 		return (procfs_domap(curp, p, pfs, uio, 0));
    286 
    287 	case Pmaps:
    288 		return (procfs_domap(curp, p, pfs, uio, 1));
    289 
    290 	case Pmem:
    291 		return (procfs_domem(curp, p, pfs, uio));
    292 
    293 	case Pcmdline:
    294 		return (procfs_docmdline(curp, p, pfs, uio));
    295 
    296 	case Pmeminfo:
    297 		return (procfs_domeminfo(curp, p, pfs, uio));
    298 
    299 	case Pcpuinfo:
    300 		return (procfs_docpuinfo(curp, p, pfs, uio));
    301 
    302 	case Pfd:
    303 		return (procfs_dofd(curp, p, pfs, uio));
    304 
    305 #ifdef __HAVE_PROCFS_MACHDEP
    306 	PROCFS_MACHDEP_NODETYPE_CASES
    307 		return (procfs_machdep_rw(curp, p, pfs, uio));
    308 #endif
    309 
    310 	default:
    311 		return (EOPNOTSUPP);
    312 	}
    313 }
    314 
    315 /*
    316  * Get a string from userland into (buf).  Strip a trailing
    317  * nl character (to allow easy access from the shell).
    318  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    319  * will automatically add a nul char at the end.
    320  *
    321  * Returns 0 on success or the following errors
    322  *
    323  * EINVAL:    file offset is non-zero.
    324  * EMSGSIZE:  message is longer than kernel buffer
    325  * EFAULT:    user i/o buffer is not addressable
    326  */
    327 int
    328 vfs_getuserstr(uio, buf, buflenp)
    329 	struct uio *uio;
    330 	char *buf;
    331 	int *buflenp;
    332 {
    333 	int xlen;
    334 	int error;
    335 
    336 	if (uio->uio_offset != 0)
    337 		return (EINVAL);
    338 
    339 	xlen = *buflenp;
    340 
    341 	/* must be able to read the whole string in one go */
    342 	if (xlen < uio->uio_resid)
    343 		return (EMSGSIZE);
    344 	xlen = uio->uio_resid;
    345 
    346 	if ((error = uiomove(buf, xlen, uio)) != 0)
    347 		return (error);
    348 
    349 	/* allow multiple writes without seeks */
    350 	uio->uio_offset = 0;
    351 
    352 	/* cleanup string and remove trailing newline */
    353 	buf[xlen] = '\0';
    354 	xlen = strlen(buf);
    355 	if (xlen > 0 && buf[xlen-1] == '\n')
    356 		buf[--xlen] = '\0';
    357 	*buflenp = xlen;
    358 
    359 	return (0);
    360 }
    361 
    362 const vfs_namemap_t *
    363 vfs_findname(nm, buf, buflen)
    364 	const vfs_namemap_t *nm;
    365 	const char *buf;
    366 	int buflen;
    367 {
    368 
    369 	for (; nm->nm_name; nm++)
    370 		if (memcmp(buf, nm->nm_name, buflen+1) == 0)
    371 			return (nm);
    372 
    373 	return (0);
    374 }
    375 
    376 /*
    377  * Initialize pfsnode hash table.
    378  */
    379 void
    380 procfs_hashinit()
    381 {
    382 	lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
    383 	pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
    384 	    M_WAITOK, &pfs_ihash);
    385 	simple_lock_init(&pfs_hash_slock);
    386 }
    387 
    388 void
    389 procfs_hashreinit()
    390 {
    391 	struct pfsnode *pp;
    392 	struct pfs_hashhead *oldhash, *hash;
    393 	u_long i, oldmask, mask, val;
    394 
    395 	hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
    396 	    &mask);
    397 
    398 	simple_lock(&pfs_hash_slock);
    399 	oldhash = pfs_hashtbl;
    400 	oldmask = pfs_ihash;
    401 	pfs_hashtbl = hash;
    402 	pfs_ihash = mask;
    403 	for (i = 0; i <= oldmask; i++) {
    404 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    405 			LIST_REMOVE(pp, pfs_hash);
    406 			val = PFSPIDHASH(pp->pfs_pid);
    407 			LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
    408 		}
    409 	}
    410 	simple_unlock(&pfs_hash_slock);
    411 	hashdone(oldhash, M_UFSMNT);
    412 }
    413 
    414 /*
    415  * Free pfsnode hash table.
    416  */
    417 void
    418 procfs_hashdone()
    419 {
    420 	hashdone(pfs_hashtbl, M_UFSMNT);
    421 }
    422 
    423 struct vnode *
    424 procfs_hashget(pid, type, fd, mp)
    425 	pid_t pid;
    426 	pfstype type;
    427 	int fd;
    428 	struct mount *mp;
    429 {
    430 	struct pfs_hashhead *ppp;
    431 	struct pfsnode *pp;
    432 	struct vnode *vp;
    433 
    434 loop:
    435 	simple_lock(&pfs_hash_slock);
    436 	ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
    437 	LIST_FOREACH(pp, ppp, pfs_hash) {
    438 		vp = PFSTOV(pp);
    439 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
    440 		    pp->pfs_fd == fd && vp->v_mount == mp) {
    441 			simple_lock(&vp->v_interlock);
    442 			simple_unlock(&pfs_hash_slock);
    443 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    444 				goto loop;
    445 			return (vp);
    446 		}
    447 	}
    448 	simple_unlock(&pfs_hash_slock);
    449 	return (NULL);
    450 }
    451 
    452 /*
    453  * Insert the pfsnode into the hash table and lock it.
    454  */
    455 void
    456 procfs_hashins(pp)
    457 	struct pfsnode *pp;
    458 {
    459 	struct pfs_hashhead *ppp;
    460 
    461 	/* lock the pfsnode, then put it on the appropriate hash list */
    462 	lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
    463 
    464 	simple_lock(&pfs_hash_slock);
    465 	ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
    466 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
    467 	simple_unlock(&pfs_hash_slock);
    468 }
    469 
    470 /*
    471  * Remove the pfsnode from the hash table.
    472  */
    473 void
    474 procfs_hashrem(pp)
    475 	struct pfsnode *pp;
    476 {
    477 	simple_lock(&pfs_hash_slock);
    478 	LIST_REMOVE(pp, pfs_hash);
    479 	simple_unlock(&pfs_hash_slock);
    480 }
    481 
    482 void
    483 procfs_revoke_vnodes(p, arg)
    484 	struct proc *p;
    485 	void *arg;
    486 {
    487 	struct pfsnode *pfs, *pnext;
    488 	struct vnode *vp;
    489 	struct mount *mp = (struct mount *)arg;
    490 	struct pfs_hashhead *ppp;
    491 
    492 	if (!(p->p_flag & P_SUGID))
    493 		return;
    494 
    495 	ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
    496 	for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
    497 		vp = PFSTOV(pfs);
    498 		pnext = LIST_NEXT(pfs, pfs_hash);
    499 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
    500 		    vp->v_mount == mp)
    501 			VOP_REVOKE(vp, REVOKEALL);
    502 	}
    503 }
    504 
    505 int
    506 procfs_getfp(pfs, fp)
    507 	struct pfsnode *pfs;
    508 	struct file **fp;
    509 {
    510 	struct proc *p = PFIND(pfs->pfs_pid);
    511 
    512 	if (p == NULL)
    513 		return ESRCH;
    514 
    515 	if (pfs->pfs_fd == -1)
    516 		return EINVAL;
    517 
    518 	if ((*fp = p->p_fd->fd_ofiles[pfs->pfs_fd]) == NULL)
    519 		return EBADF;
    520 	return 0;
    521 }
    522