Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.49
      1 /*	$NetBSD: procfs_subr.c,v 1.49 2003/04/17 19:04:25 jdolecek 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.49 2003/04/17 19:04:25 jdolecek 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 			struct proc *pown;
    154 
    155 			/* XXX can procfs_getfp() ever fail here? */
    156 			if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
    157 				goto bad;
    158 			FILE_USE(fp);
    159 
    160 			pfs->pfs_mode = S_IRUSR|S_IWUSR;
    161 			switch (fp->f_type) {
    162 			case DTYPE_VNODE:
    163 				vxp = (struct vnode *)fp->f_data;
    164 				vp->v_type = vxp->v_type;
    165 				break;
    166 			case DTYPE_PIPE:
    167 				vp->v_type = VFIFO;
    168 				break;
    169 			case DTYPE_SOCKET:
    170 				vp->v_type = VSOCK;
    171 				break;
    172 			default:
    173 				error = EOPNOTSUPP;
    174 				FILE_UNUSE(fp, pown);
    175 				goto bad;
    176 			}
    177 			FILE_UNUSE(fp, pown);
    178 		}
    179 		break;
    180 
    181 	case Pfile:	/* /proc/N/file = -rw------- */
    182 	case Pmem:	/* /proc/N/mem = -rw------- */
    183 	case Pregs:	/* /proc/N/regs = -rw------- */
    184 	case Pfpregs:	/* /proc/N/fpregs = -rw------- */
    185 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    186 		vp->v_type = VREG;
    187 		break;
    188 
    189 	case Pctl:	/* /proc/N/ctl = --w------ */
    190 	case Pnote:	/* /proc/N/note = --w------ */
    191 	case Pnotepg:	/* /proc/N/notepg = --w------ */
    192 		pfs->pfs_mode = S_IWUSR;
    193 		vp->v_type = VREG;
    194 		break;
    195 
    196 	case Pmap:	/* /proc/N/map = -r--r--r-- */
    197 	case Pmaps:	/* /proc/N/maps = -r--r--r-- */
    198 	case Pstatus:	/* /proc/N/status = -r--r--r-- */
    199 	case Pcmdline:	/* /proc/N/cmdline = -r--r--r-- */
    200 	case Pmeminfo:	/* /proc/meminfo = -r--r--r-- */
    201 	case Pcpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
    202 	case Puptime:	/* /proc/uptime = -r--r--r-- */
    203 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    204 		vp->v_type = VREG;
    205 		break;
    206 
    207 #ifdef __HAVE_PROCFS_MACHDEP
    208 	PROCFS_MACHDEP_NODETYPE_CASES
    209 		procfs_machdep_allocvp(vp);
    210 		break;
    211 #endif
    212 
    213 	default:
    214 		panic("procfs_allocvp");
    215 	}
    216 
    217 	procfs_hashins(pfs);
    218 	uvm_vnp_setsize(vp, 0);
    219 	lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    220 
    221 	*vpp = vp;
    222 	return (0);
    223 
    224  bad:
    225 	lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    226 	FREE(pfs, M_TEMP);
    227 	ungetnewvnode(vp);
    228 	return (error);
    229 }
    230 
    231 int
    232 procfs_freevp(vp)
    233 	struct vnode *vp;
    234 {
    235 	struct pfsnode *pfs = VTOPFS(vp);
    236 
    237 	procfs_hashrem(pfs);
    238 
    239 	FREE(vp->v_data, M_TEMP);
    240 	vp->v_data = 0;
    241 	return (0);
    242 }
    243 
    244 int
    245 procfs_rw(v)
    246 	void *v;
    247 {
    248 	struct vop_read_args *ap = v;
    249 	struct vnode *vp = ap->a_vp;
    250 	struct uio *uio = ap->a_uio;
    251 	struct proc *curp = uio->uio_procp;
    252 	struct pfsnode *pfs = VTOPFS(vp);
    253 	struct lwp *l;
    254 	struct proc *p;
    255 
    256 	p = PFIND(pfs->pfs_pid);
    257 	if (p == 0)
    258 		return (EINVAL);
    259 
    260 	/* XXX NJWLWP
    261 	 * The entire procfs interface needs work to be useful to
    262 	 * a process with multiple LWPs. For the moment, we'll
    263 	 * just kluge this and fail on others.
    264 	 */
    265 	l = proc_representative_lwp(p);
    266 
    267 	switch (pfs->pfs_type) {
    268 	case Pregs:
    269 	case Pfpregs:
    270 	case Pmem:
    271 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    272 	PROCFS_MACHDEP_PROTECT_CASES
    273 #endif
    274 		/*
    275 		 * Do not allow init to be modified while in secure mode; it
    276 		 * could be duped into changing the security level.
    277 		 */
    278 		if (uio->uio_rw == UIO_WRITE &&
    279 		    p == initproc && securelevel > -1)
    280 			return (EPERM);
    281 		break;
    282 
    283 	default:
    284 		break;
    285 	}
    286 
    287 	switch (pfs->pfs_type) {
    288 	case Pnote:
    289 	case Pnotepg:
    290 		return (procfs_donote(curp, p, pfs, uio));
    291 
    292 	case Pregs:
    293 		return (procfs_doregs(curp, l, pfs, uio));
    294 
    295 	case Pfpregs:
    296 		return (procfs_dofpregs(curp, l, pfs, uio));
    297 
    298 	case Pctl:
    299 		return (procfs_doctl(curp, l, pfs, uio));
    300 
    301 	case Pstatus:
    302 		return (procfs_dostatus(curp, l, pfs, uio));
    303 
    304 	case Pmap:
    305 		return (procfs_domap(curp, p, pfs, uio, 0));
    306 
    307 	case Pmaps:
    308 		return (procfs_domap(curp, p, pfs, uio, 1));
    309 
    310 	case Pmem:
    311 		return (procfs_domem(curp, p, pfs, uio));
    312 
    313 	case Pcmdline:
    314 		return (procfs_docmdline(curp, p, pfs, uio));
    315 
    316 	case Pmeminfo:
    317 		return (procfs_domeminfo(curp, p, pfs, uio));
    318 
    319 	case Pcpuinfo:
    320 		return (procfs_docpuinfo(curp, p, pfs, uio));
    321 
    322 	case Pfd:
    323 		return (procfs_dofd(curp, p, pfs, uio));
    324 
    325 	case Puptime:
    326 		return (procfs_douptime(curp, p, pfs, uio));
    327 
    328 #ifdef __HAVE_PROCFS_MACHDEP
    329 	PROCFS_MACHDEP_NODETYPE_CASES
    330 		return (procfs_machdep_rw(curp, l, pfs, uio));
    331 #endif
    332 
    333 	default:
    334 		return (EOPNOTSUPP);
    335 	}
    336 }
    337 
    338 /*
    339  * Get a string from userland into (buf).  Strip a trailing
    340  * nl character (to allow easy access from the shell).
    341  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    342  * will automatically add a nul char at the end.
    343  *
    344  * Returns 0 on success or the following errors
    345  *
    346  * EINVAL:    file offset is non-zero.
    347  * EMSGSIZE:  message is longer than kernel buffer
    348  * EFAULT:    user i/o buffer is not addressable
    349  */
    350 int
    351 vfs_getuserstr(uio, buf, buflenp)
    352 	struct uio *uio;
    353 	char *buf;
    354 	int *buflenp;
    355 {
    356 	int xlen;
    357 	int error;
    358 
    359 	if (uio->uio_offset != 0)
    360 		return (EINVAL);
    361 
    362 	xlen = *buflenp;
    363 
    364 	/* must be able to read the whole string in one go */
    365 	if (xlen < uio->uio_resid)
    366 		return (EMSGSIZE);
    367 	xlen = uio->uio_resid;
    368 
    369 	if ((error = uiomove(buf, xlen, uio)) != 0)
    370 		return (error);
    371 
    372 	/* allow multiple writes without seeks */
    373 	uio->uio_offset = 0;
    374 
    375 	/* cleanup string and remove trailing newline */
    376 	buf[xlen] = '\0';
    377 	xlen = strlen(buf);
    378 	if (xlen > 0 && buf[xlen-1] == '\n')
    379 		buf[--xlen] = '\0';
    380 	*buflenp = xlen;
    381 
    382 	return (0);
    383 }
    384 
    385 const vfs_namemap_t *
    386 vfs_findname(nm, buf, buflen)
    387 	const vfs_namemap_t *nm;
    388 	const char *buf;
    389 	int buflen;
    390 {
    391 
    392 	for (; nm->nm_name; nm++)
    393 		if (memcmp(buf, nm->nm_name, buflen+1) == 0)
    394 			return (nm);
    395 
    396 	return (0);
    397 }
    398 
    399 /*
    400  * Initialize pfsnode hash table.
    401  */
    402 void
    403 procfs_hashinit()
    404 {
    405 	lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
    406 	pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
    407 	    M_WAITOK, &pfs_ihash);
    408 	simple_lock_init(&pfs_hash_slock);
    409 }
    410 
    411 void
    412 procfs_hashreinit()
    413 {
    414 	struct pfsnode *pp;
    415 	struct pfs_hashhead *oldhash, *hash;
    416 	u_long i, oldmask, mask, val;
    417 
    418 	hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
    419 	    &mask);
    420 
    421 	simple_lock(&pfs_hash_slock);
    422 	oldhash = pfs_hashtbl;
    423 	oldmask = pfs_ihash;
    424 	pfs_hashtbl = hash;
    425 	pfs_ihash = mask;
    426 	for (i = 0; i <= oldmask; i++) {
    427 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    428 			LIST_REMOVE(pp, pfs_hash);
    429 			val = PFSPIDHASH(pp->pfs_pid);
    430 			LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
    431 		}
    432 	}
    433 	simple_unlock(&pfs_hash_slock);
    434 	hashdone(oldhash, M_UFSMNT);
    435 }
    436 
    437 /*
    438  * Free pfsnode hash table.
    439  */
    440 void
    441 procfs_hashdone()
    442 {
    443 	hashdone(pfs_hashtbl, M_UFSMNT);
    444 }
    445 
    446 struct vnode *
    447 procfs_hashget(pid, type, fd, mp)
    448 	pid_t pid;
    449 	pfstype type;
    450 	int fd;
    451 	struct mount *mp;
    452 {
    453 	struct pfs_hashhead *ppp;
    454 	struct pfsnode *pp;
    455 	struct vnode *vp;
    456 
    457 loop:
    458 	simple_lock(&pfs_hash_slock);
    459 	ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
    460 	LIST_FOREACH(pp, ppp, pfs_hash) {
    461 		vp = PFSTOV(pp);
    462 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
    463 		    pp->pfs_fd == fd && vp->v_mount == mp) {
    464 			simple_lock(&vp->v_interlock);
    465 			simple_unlock(&pfs_hash_slock);
    466 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    467 				goto loop;
    468 			return (vp);
    469 		}
    470 	}
    471 	simple_unlock(&pfs_hash_slock);
    472 	return (NULL);
    473 }
    474 
    475 /*
    476  * Insert the pfsnode into the hash table and lock it.
    477  */
    478 void
    479 procfs_hashins(pp)
    480 	struct pfsnode *pp;
    481 {
    482 	struct pfs_hashhead *ppp;
    483 
    484 	/* lock the pfsnode, then put it on the appropriate hash list */
    485 	lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
    486 
    487 	simple_lock(&pfs_hash_slock);
    488 	ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
    489 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
    490 	simple_unlock(&pfs_hash_slock);
    491 }
    492 
    493 /*
    494  * Remove the pfsnode from the hash table.
    495  */
    496 void
    497 procfs_hashrem(pp)
    498 	struct pfsnode *pp;
    499 {
    500 	simple_lock(&pfs_hash_slock);
    501 	LIST_REMOVE(pp, pfs_hash);
    502 	simple_unlock(&pfs_hash_slock);
    503 }
    504 
    505 void
    506 procfs_revoke_vnodes(p, arg)
    507 	struct proc *p;
    508 	void *arg;
    509 {
    510 	struct pfsnode *pfs, *pnext;
    511 	struct vnode *vp;
    512 	struct mount *mp = (struct mount *)arg;
    513 	struct pfs_hashhead *ppp;
    514 
    515 	if (!(p->p_flag & P_SUGID))
    516 		return;
    517 
    518 	ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
    519 	for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
    520 		vp = PFSTOV(pfs);
    521 		pnext = LIST_NEXT(pfs, pfs_hash);
    522 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
    523 		    vp->v_mount == mp)
    524 			VOP_REVOKE(vp, REVOKEALL);
    525 	}
    526 }
    527 
    528 int
    529 procfs_getfp(pfs, pown, fp)
    530 	struct pfsnode *pfs;
    531 	struct proc **pown;
    532 	struct file **fp;
    533 {
    534 	struct proc *p = PFIND(pfs->pfs_pid);
    535 
    536 	if (p == NULL)
    537 		return ESRCH;
    538 
    539 	if (pfs->pfs_fd == -1)
    540 		return EINVAL;
    541 
    542 	if ((*fp = fd_getfile(p->p_fd, pfs->pfs_fd)) == NULL)
    543 		return EBADF;
    544 
    545 	*pown = p;
    546 	return 0;
    547 }
    548