Home | History | Annotate | Line # | Download | only in procfs
procfs_subr.c revision 1.36.2.7
      1 /*	$NetBSD: procfs_subr.c,v 1.36.2.7 2002/04/01 21:31:35 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.7 2002/04/01 21:31:35 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 	l = proc_representative_lwp(p);
    223 
    224 	switch (pfs->pfs_type) {
    225 	case Pregs:
    226 	case Pfpregs:
    227 	case Pmem:
    228 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
    229 	PROCFS_MACHDEP_PROTECT_CASES
    230 #endif
    231 		/*
    232 		 * Do not allow init to be modified while in secure mode; it
    233 		 * could be duped into changing the security level.
    234 		 */
    235 		if (uio->uio_rw == UIO_WRITE &&
    236 		    p == initproc && securelevel > -1)
    237 			return (EPERM);
    238 		break;
    239 
    240 	default:
    241 		break;
    242 	}
    243 
    244 	switch (pfs->pfs_type) {
    245 	case Pnote:
    246 	case Pnotepg:
    247 		return (procfs_donote(curp, p, pfs, uio));
    248 
    249 	case Pregs:
    250 		return (procfs_doregs(curp, l, pfs, uio));
    251 
    252 	case Pfpregs:
    253 		return (procfs_dofpregs(curp, l, pfs, uio));
    254 
    255 	case Pctl:
    256 		return (procfs_doctl(curp, l, pfs, uio));
    257 
    258 	case Pstatus:
    259 		return (procfs_dostatus(curp, l, pfs, uio));
    260 
    261 	case Pmap:
    262 		return (procfs_domap(curp, p, pfs, uio, 0));
    263 
    264 	case Pmaps:
    265 		return (procfs_domap(curp, p, pfs, uio, 1));
    266 
    267 	case Pmem:
    268 		return (procfs_domem(curp, p, pfs, uio));
    269 
    270 	case Pcmdline:
    271 		return (procfs_docmdline(curp, p, pfs, uio));
    272 
    273 	case Pmeminfo:
    274 		return (procfs_domeminfo(curp, p, pfs, uio));
    275 
    276 	case Pcpuinfo:
    277 		return (procfs_docpuinfo(curp, p, pfs, uio));
    278 
    279 #ifdef __HAVE_PROCFS_MACHDEP
    280 	PROCFS_MACHDEP_NODETYPE_CASES
    281 		return (procfs_machdep_rw(curp, l, pfs, uio));
    282 #endif
    283 
    284 	default:
    285 		return (EOPNOTSUPP);
    286 	}
    287 }
    288 
    289 /*
    290  * Get a string from userland into (buf).  Strip a trailing
    291  * nl character (to allow easy access from the shell).
    292  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
    293  * will automatically add a nul char at the end.
    294  *
    295  * Returns 0 on success or the following errors
    296  *
    297  * EINVAL:    file offset is non-zero.
    298  * EMSGSIZE:  message is longer than kernel buffer
    299  * EFAULT:    user i/o buffer is not addressable
    300  */
    301 int
    302 vfs_getuserstr(uio, buf, buflenp)
    303 	struct uio *uio;
    304 	char *buf;
    305 	int *buflenp;
    306 {
    307 	int xlen;
    308 	int error;
    309 
    310 	if (uio->uio_offset != 0)
    311 		return (EINVAL);
    312 
    313 	xlen = *buflenp;
    314 
    315 	/* must be able to read the whole string in one go */
    316 	if (xlen < uio->uio_resid)
    317 		return (EMSGSIZE);
    318 	xlen = uio->uio_resid;
    319 
    320 	if ((error = uiomove(buf, xlen, uio)) != 0)
    321 		return (error);
    322 
    323 	/* allow multiple writes without seeks */
    324 	uio->uio_offset = 0;
    325 
    326 	/* cleanup string and remove trailing newline */
    327 	buf[xlen] = '\0';
    328 	xlen = strlen(buf);
    329 	if (xlen > 0 && buf[xlen-1] == '\n')
    330 		buf[--xlen] = '\0';
    331 	*buflenp = xlen;
    332 
    333 	return (0);
    334 }
    335 
    336 const vfs_namemap_t *
    337 vfs_findname(nm, buf, buflen)
    338 	const vfs_namemap_t *nm;
    339 	const char *buf;
    340 	int buflen;
    341 {
    342 
    343 	for (; nm->nm_name; nm++)
    344 		if (memcmp(buf, nm->nm_name, buflen+1) == 0)
    345 			return (nm);
    346 
    347 	return (0);
    348 }
    349 
    350 /*
    351  * Initialize pfsnode hash table.
    352  */
    353 void
    354 procfs_hashinit()
    355 {
    356 	lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
    357 	pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
    358 	    M_WAITOK, &pfs_ihash);
    359 	simple_lock_init(&pfs_hash_slock);
    360 }
    361 
    362 void
    363 procfs_hashreinit()
    364 {
    365 	struct pfsnode *pp;
    366 	struct pfs_hashhead *oldhash, *hash;
    367 	u_long oldmask, mask, val;
    368 	int i;
    369 
    370 	hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
    371 	    &mask);
    372 
    373 	simple_lock(&pfs_hash_slock);
    374 	oldhash = pfs_hashtbl;
    375 	oldmask = pfs_ihash;
    376 	pfs_hashtbl = hash;
    377 	pfs_ihash = mask;
    378 	for (i = 0; i <= oldmask; i++) {
    379 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
    380 			LIST_REMOVE(pp, pfs_hash);
    381 			val = PFSPIDHASH(pp->pfs_pid);
    382 			LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
    383 		}
    384 	}
    385 	simple_unlock(&pfs_hash_slock);
    386 	hashdone(oldhash, M_UFSMNT);
    387 }
    388 
    389 /*
    390  * Free pfsnode hash table.
    391  */
    392 void
    393 procfs_hashdone()
    394 {
    395 	hashdone(pfs_hashtbl, M_UFSMNT);
    396 }
    397 
    398 struct vnode *
    399 procfs_hashget(pid, type, mp)
    400 	pid_t pid;
    401 	pfstype type;
    402 	struct mount *mp;
    403 {
    404 	struct pfs_hashhead *ppp;
    405 	struct pfsnode *pp;
    406 	struct vnode *vp;
    407 
    408 loop:
    409 	simple_lock(&pfs_hash_slock);
    410 	ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
    411 	LIST_FOREACH(pp, ppp, pfs_hash) {
    412 		vp = PFSTOV(pp);
    413 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
    414 		    vp->v_mount == mp) {
    415 			simple_lock(&vp->v_interlock);
    416 			simple_unlock(&pfs_hash_slock);
    417 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
    418 				goto loop;
    419 			return (vp);
    420 		}
    421 	}
    422 	simple_unlock(&pfs_hash_slock);
    423 	return (NULL);
    424 }
    425 
    426 /*
    427  * Insert the pfsnode into the hash table and lock it.
    428  */
    429 void
    430 procfs_hashins(pp)
    431 	struct pfsnode *pp;
    432 {
    433 	struct pfs_hashhead *ppp;
    434 
    435 	/* lock the pfsnode, then put it on the appropriate hash list */
    436 	lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
    437 
    438 	simple_lock(&pfs_hash_slock);
    439 	ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
    440 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
    441 	simple_unlock(&pfs_hash_slock);
    442 }
    443 
    444 /*
    445  * Remove the pfsnode from the hash table.
    446  */
    447 void
    448 procfs_hashrem(pp)
    449 	struct pfsnode *pp;
    450 {
    451 	simple_lock(&pfs_hash_slock);
    452 	LIST_REMOVE(pp, pfs_hash);
    453 	simple_unlock(&pfs_hash_slock);
    454 }
    455 
    456 void
    457 procfs_revoke_vnodes(p, arg)
    458 	struct proc *p;
    459 	void *arg;
    460 {
    461 	struct pfsnode *pfs, *pnext;
    462 	struct vnode *vp;
    463 	struct mount *mp = (struct mount *)arg;
    464 	struct pfs_hashhead *ppp;
    465 
    466 	if (!(p->p_flag & P_SUGID))
    467 		return;
    468 
    469 	ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
    470 	for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
    471 		vp = PFSTOV(pfs);
    472 		pnext = LIST_NEXT(pfs, pfs_hash);
    473 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
    474 		    vp->v_mount == mp)
    475 			VOP_REVOKE(vp, REVOKEALL);
    476 	}
    477 }
    478