Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.36.2.5
      1 /*	$NetBSD: procfs_subr.c,v 1.36.2.5 2002/01/08 00:33:42 nathanw 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.36.2.5 2002/01/08 00:33:42 nathanw 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 
     55 #include <miscfs/procfs/procfs.h>
     56 
     57 void procfs_hashins __P((struct pfsnode *));
     58 void procfs_hashrem __P((struct pfsnode *));
     59 struct vnode *procfs_hashget __P((pid_t, pfstype, struct mount *));
     60 
     61 LIST_HEAD(pfs_hashhead, pfsnode) *pfs_hashtbl;
     62 u_long	pfs_ihash;	/* size of hash table - 1 */
     63 #define PFSPIDHASH(pid)	((pid) & pfs_ihash)
     64 
     65 struct lock pfs_hashlock;
     66 struct simplelock pfs_hash_slock;
     67 
     68 #define	ISSET(t, f)	((t) & (f))
     69 
     70 /*
     71  * allocate a pfsnode/vnode pair.  the vnode is
     72  * referenced, and locked.
     73  *
     74  * the pid, pfs_type, and mount point uniquely
     75  * identify a pfsnode.  the mount point is needed
     76  * because someone might mount this filesystem
     77  * twice.
     78  *
     79  * all pfsnodes are maintained on a singly-linked
     80  * list.  new nodes are only allocated when they cannot
     81  * be found on this list.  entries on the list are
     82  * removed when the vfs reclaim entry is called.
     83  *
     84  * a single lock is kept for the entire list.  this is
     85  * needed because the getnewvnode() function can block
     86  * waiting for a vnode to become free, in which case there
     87  * may be more than one process trying to get the same
     88  * vnode.  this lock is only taken if we are going to
     89  * call getnewvnode, since the kernel itself is single-threaded.
     90  *
     91  * if an entry is found on the list, then call vget() to
     92  * take a reference.  this is done because there may be
     93  * zero references to it and so it needs to removed from
     94  * the vnode free list.
     95  */
     96 int
     97 procfs_allocvp(mp, vpp, pid, pfs_type)
     98 	struct mount *mp;
     99 	struct vnode **vpp;
    100 	long pid;
    101 	pfstype pfs_type;
    102 {
    103 	struct pfsnode *pfs;
    104 	struct vnode *vp;
    105 	int error;
    106 
    107 	do {
    108 		if ((*vpp = procfs_hashget(pid, pfs_type, mp)) != NULL)
    109 			return (0);
    110 	} while (lockmgr(&pfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
    111 
    112 	if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp)) != 0) {
    113 		*vpp = NULL;
    114 		lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    115 		return (error);
    116 	}
    117 	vp = *vpp;
    118 
    119 	MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
    120 	vp->v_data = pfs;
    121 
    122 	pfs->pfs_pid = (pid_t) pid;
    123 	pfs->pfs_type = pfs_type;
    124 	pfs->pfs_vnode = vp;
    125 	pfs->pfs_flags = 0;
    126 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
    127 
    128 	switch (pfs_type) {
    129 	case Proot:	/* /proc = dr-xr-xr-x */
    130 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    131 		vp->v_type = VDIR;
    132 		vp->v_flag = VROOT;
    133 		break;
    134 
    135 	case Pcurproc:	/* /proc/curproc = lr-xr-xr-x */
    136 	case Pself:	/* /proc/self    = lr-xr-xr-x */
    137 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    138 		vp->v_type = VLNK;
    139 		break;
    140 
    141 	case Pproc:	/* /proc/N = dr-xr-xr-x */
    142 		pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
    143 		vp->v_type = VDIR;
    144 		break;
    145 
    146 	case Pfile:	/* /proc/N/file = -rw------- */
    147 	case Pmem:	/* /proc/N/mem = -rw------- */
    148 	case Pregs:	/* /proc/N/regs = -rw------- */
    149 	case Pfpregs:	/* /proc/N/fpregs = -rw------- */
    150 		pfs->pfs_mode = S_IRUSR|S_IWUSR;
    151 		vp->v_type = VREG;
    152 		break;
    153 
    154 	case Pctl:	/* /proc/N/ctl = --w------ */
    155 	case Pnote:	/* /proc/N/note = --w------ */
    156 	case Pnotepg:	/* /proc/N/notepg = --w------ */
    157 		pfs->pfs_mode = S_IWUSR;
    158 		vp->v_type = VREG;
    159 		break;
    160 
    161 	case Pmap:	/* /proc/N/map = -r--r--r-- */
    162 	case Pmaps:	/* /proc/N/maps = -r--r--r-- */
    163 	case Pstatus:	/* /proc/N/status = -r--r--r-- */
    164 	case Pcmdline:	/* /proc/N/cmdline = -r--r--r-- */
    165 	case Pmeminfo:	/* /proc/meminfo = -r--r--r-- */
    166 	case Pcpuinfo:	/* /proc/cpuinfo = -r--r--r-- */
    167 		pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
    168 		vp->v_type = VREG;
    169 		break;
    170 
    171 #ifdef __HAVE_PROCFS_MACHDEP
    172 	PROCFS_MACHDEP_NODETYPE_CASES
    173 		procfs_machdep_allocvp(vp);
    174 		break;
    175 #endif
    176 
    177 	default:
    178 		panic("procfs_allocvp");
    179 	}
    180 
    181 	procfs_hashins(pfs);
    182 	uvm_vnp_setsize(vp, 0);
    183 	lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
    184 
    185 	return (error);
    186 }
    187 
    188 int
    189 procfs_freevp(vp)
    190 	struct vnode *vp;
    191 {
    192 	struct pfsnode *pfs = VTOPFS(vp);
    193 
    194 	procfs_hashrem(pfs);
    195 
    196 	FREE(vp->v_data, M_TEMP);
    197 	vp->v_data = 0;
    198 	return (0);
    199 }
    200 
    201 int
    202 procfs_rw(v)
    203 	void *v;
    204 {
    205 	struct vop_read_args *ap = v;
    206 	struct vnode *vp = ap->a_vp;
    207 	struct uio *uio = ap->a_uio;
    208 	struct proc *curp = uio->uio_procp;
    209 	struct pfsnode *pfs = VTOPFS(vp);
    210 	struct lwp *l;
    211 	struct proc *p;
    212 
    213 	p = PFIND(pfs->pfs_pid);
    214 	if (p == 0)
    215 		return (EINVAL);
    216 
    217 	/* XXX NJWLWP
    218 	 * The entire procfs interface needs work to be useful to
    219 	 * a process with multiple LWPs. For the moment, we'll
    220 	 * just kluge this and fail on others.
    221 	 */
    222 
    223 	if (p->p_nlwps > 1)
    224 		switch (pfs->pfs_type) {
    225 		case Pnote:
    226 		case Pnotepg:
    227 		case Pstatus:
    228 		case Pmap:
    229 		case Pmem:
    230 		case Pcmdline:
    231 			break;
    232 		case Pregs:
    233 		case Pfpregs:
    234 		case Pctl:
    235 		default:
    236 			return (EOPNOTSUPP);
    237 		}
    238 
    239 	l = LIST_FIRST(&p->p_lwps);
    240 
    241 	switch (pfs->pfs_type) {
    242 	case Pregs:
    243 	case Pfpregs:
    244 	case Pmem:
    245 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    246 	PROCFS_MACHDEP_PROTECT_CASES
    247 #endif
    248 		/*
    249 		 * Do not allow init to be modified while in secure mode; it
    250 		 * could be duped into changing the security level.
    251 		 */
    252 		if (uio->uio_rw == UIO_WRITE &&
    253 		    p == initproc && securelevel > -1)
    254 			return (EPERM);
    255 		break;
    256 
    257 	default:
    258 		break;
    259 	}
    260 
    261 	switch (pfs->pfs_type) {
    262 	case Pnote:
    263 	case Pnotepg:
    264 		return (procfs_donote(curp, p, pfs, uio));
    265 
    266 	case Pregs:
    267 		return (procfs_doregs(curp, l, pfs, uio));
    268 
    269 	case Pfpregs:
    270 		return (procfs_dofpregs(curp, l, pfs, uio));
    271 
    272 	case Pctl:
    273 		return (procfs_doctl(curp, l, pfs, uio));
    274 
    275 	case Pstatus:
    276 		return (procfs_dostatus(curp, l, pfs, uio));
    277 
    278 	case Pmap:
    279 		return (procfs_domap(curp, p, pfs, uio, 0));
    280 
    281 	case Pmaps:
    282 		return (procfs_domap(curp, p, pfs, uio, 1));
    283 
    284 	case Pmem:
    285 		return (procfs_domem(curp, l, pfs, uio));
    286 
    287 	case Pcmdline:
    288 		return (procfs_docmdline(curp, p, pfs, uio));
    289 
    290 	case Pmeminfo:
    291 		return (procfs_domeminfo(curp, p, pfs, uio));
    292 
    293 	case Pcpuinfo:
    294 		return (procfs_docpuinfo(curp, p, pfs, uio));
    295 
    296 #ifdef __HAVE_PROCFS_MACHDEP
    297 	PROCFS_MACHDEP_NODETYPE_CASES
    298 		return (procfs_machdep_rw(curp, p, pfs, uio));
    299 #endif
    300 
    301 	default:
    302 		return (EOPNOTSUPP);
    303 	}
    304 }
    305 
    306 /*
    307  * Get a string from userland into (buf).  Strip a trailing
    308  * nl character (to allow easy access from the shell).
    309  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    310  * will automatically add a nul char at the end.
    311  *
    312  * Returns 0 on success or the following errors
    313  *
    314  * EINVAL:    file offset is non-zero.
    315  * EMSGSIZE:  message is longer than kernel buffer
    316  * EFAULT:    user i/o buffer is not addressable
    317  */
    318 int
    319 vfs_getuserstr(uio, buf, buflenp)
    320 	struct uio *uio;
    321 	char *buf;
    322 	int *buflenp;
    323 {
    324 	int xlen;
    325 	int error;
    326 
    327 	if (uio->uio_offset != 0)
    328 		return (EINVAL);
    329 
    330 	xlen = *buflenp;
    331 
    332 	/* must be able to read the whole string in one go */
    333 	if (xlen < uio->uio_resid)
    334 		return (EMSGSIZE);
    335 	xlen = uio->uio_resid;
    336 
    337 	if ((error = uiomove(buf, xlen, uio)) != 0)
    338 		return (error);
    339 
    340 	/* allow multiple writes without seeks */
    341 	uio->uio_offset = 0;
    342 
    343 	/* cleanup string and remove trailing newline */
    344 	buf[xlen] = '\0';
    345 	xlen = strlen(buf);
    346 	if (xlen > 0 && buf[xlen-1] == '\n')
    347 		buf[--xlen] = '\0';
    348 	*buflenp = xlen;
    349 
    350 	return (0);
    351 }
    352 
    353 const vfs_namemap_t *
    354 vfs_findname(nm, buf, buflen)
    355 	const vfs_namemap_t *nm;
    356 	const char *buf;
    357 	int buflen;
    358 {
    359 
    360 	for (; nm->nm_name; nm++)
    361 		if (memcmp(buf, nm->nm_name, buflen+1) == 0)
    362 			return (nm);
    363 
    364 	return (0);
    365 }
    366 
    367 /*
    368  * Initialize pfsnode hash table.
    369  */
    370 void
    371 procfs_hashinit()
    372 {
    373 	lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
    374 	pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
    375 	    M_WAITOK, &pfs_ihash);
    376 	simple_lock_init(&pfs_hash_slock);
    377 }
    378 
    379 void
    380 procfs_hashreinit()
    381 {
    382 	struct pfsnode *pp;
    383 	struct pfs_hashhead *oldhash, *hash;
    384 	u_long oldmask, mask, val;
    385 	int i;
    386 
    387 	hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
    388 	    &mask);
    389 
    390 	simple_lock(&pfs_hash_slock);
    391 	oldhash = pfs_hashtbl;
    392 	oldmask = pfs_ihash;
    393 	pfs_hashtbl = hash;
    394 	pfs_ihash = mask;
    395 	for (i = 0; i <= oldmask; i++) {
    396 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    397 			LIST_REMOVE(pp, pfs_hash);
    398 			val = PFSPIDHASH(pp->pfs_pid);
    399 			LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
    400 		}
    401 	}
    402 	simple_unlock(&pfs_hash_slock);
    403 	hashdone(oldhash, M_UFSMNT);
    404 }
    405 
    406 /*
    407  * Free pfsnode hash table.
    408  */
    409 void
    410 procfs_hashdone()
    411 {
    412 	hashdone(pfs_hashtbl, M_UFSMNT);
    413 }
    414 
    415 struct vnode *
    416 procfs_hashget(pid, type, mp)
    417 	pid_t pid;
    418 	pfstype type;
    419 	struct mount *mp;
    420 {
    421 	struct pfs_hashhead *ppp;
    422 	struct pfsnode *pp;
    423 	struct vnode *vp;
    424 
    425 loop:
    426 	simple_lock(&pfs_hash_slock);
    427 	ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
    428 	LIST_FOREACH(pp, ppp, pfs_hash) {
    429 		vp = PFSTOV(pp);
    430 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
    431 		    vp->v_mount == mp) {
    432 			simple_lock(&vp->v_interlock);
    433 			simple_unlock(&pfs_hash_slock);
    434 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    435 				goto loop;
    436 			return (vp);
    437 		}
    438 	}
    439 	simple_unlock(&pfs_hash_slock);
    440 	return (NULL);
    441 }
    442 
    443 /*
    444  * Insert the pfsnode into the hash table and lock it.
    445  */
    446 void
    447 procfs_hashins(pp)
    448 	struct pfsnode *pp;
    449 {
    450 	struct pfs_hashhead *ppp;
    451 
    452 	/* lock the pfsnode, then put it on the appropriate hash list */
    453 	lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
    454 
    455 	simple_lock(&pfs_hash_slock);
    456 	ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
    457 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
    458 	simple_unlock(&pfs_hash_slock);
    459 }
    460 
    461 /*
    462  * Remove the pfsnode from the hash table.
    463  */
    464 void
    465 procfs_hashrem(pp)
    466 	struct pfsnode *pp;
    467 {
    468 	simple_lock(&pfs_hash_slock);
    469 	LIST_REMOVE(pp, pfs_hash);
    470 	simple_unlock(&pfs_hash_slock);
    471 }
    472 
    473 void
    474 procfs_revoke_vnodes(p, arg)
    475 	struct proc *p;
    476 	void *arg;
    477 {
    478 	struct pfsnode *pfs, *pnext;
    479 	struct vnode *vp;
    480 	struct mount *mp = (struct mount *)arg;
    481 	struct pfs_hashhead *ppp;
    482 
    483 	if (!(p->p_flag & P_SUGID))
    484 		return;
    485 
    486 	ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
    487 	for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
    488 		vp = PFSTOV(pfs);
    489 		pnext = LIST_NEXT(pfs, pfs_hash);
    490 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
    491 		    vp->v_mount == mp)
    492 			VOP_REVOKE(vp, REVOKEALL);
    493 	}
    494 }
    495