xref: /netbsd-src/sys/miscfs/procfs/procfs_subr.c (revision 06be8101a16cc95f40783b3cb7afd12112103a9a)
1 /*	$NetBSD: procfs_subr.c,v 1.39 2001/11/10 13:33:44 lukem 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.39 2001/11/10 13:33:44 lukem 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 	default:
172 		panic("procfs_allocvp");
173 	}
174 
175 	procfs_hashins(pfs);
176 	uvm_vnp_setsize(vp, 0);
177 	lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
178 
179 	return (error);
180 }
181 
182 int
183 procfs_freevp(vp)
184 	struct vnode *vp;
185 {
186 	struct pfsnode *pfs = VTOPFS(vp);
187 
188 	procfs_hashrem(pfs);
189 
190 	FREE(vp->v_data, M_TEMP);
191 	vp->v_data = 0;
192 	return (0);
193 }
194 
195 int
196 procfs_rw(v)
197 	void *v;
198 {
199 	struct vop_read_args *ap = v;
200 	struct vnode *vp = ap->a_vp;
201 	struct uio *uio = ap->a_uio;
202 	struct proc *curp = uio->uio_procp;
203 	struct pfsnode *pfs = VTOPFS(vp);
204 	struct proc *p;
205 
206 	p = PFIND(pfs->pfs_pid);
207 	if (p == 0)
208 		return (EINVAL);
209 
210 	switch (pfs->pfs_type) {
211 	case Pregs:
212 	case Pfpregs:
213 	case Pmem:
214 		/*
215 		 * Do not allow init to be modified while in secure mode; it
216 		 * could be duped into changing the security level.
217 		 */
218 		if (uio->uio_rw == UIO_WRITE &&
219 		    p == initproc && securelevel > -1)
220 			return (EPERM);
221 		break;
222 
223 	default:
224 		break;
225 	}
226 
227 	switch (pfs->pfs_type) {
228 	case Pnote:
229 	case Pnotepg:
230 		return (procfs_donote(curp, p, pfs, uio));
231 
232 	case Pregs:
233 		return (procfs_doregs(curp, p, pfs, uio));
234 
235 	case Pfpregs:
236 		return (procfs_dofpregs(curp, p, pfs, uio));
237 
238 	case Pctl:
239 		return (procfs_doctl(curp, p, pfs, uio));
240 
241 	case Pstatus:
242 		return (procfs_dostatus(curp, p, pfs, uio));
243 
244 	case Pmap:
245 		return (procfs_domap(curp, p, pfs, uio, 0));
246 
247 	case Pmaps:
248 		return (procfs_domap(curp, p, pfs, uio, 1));
249 
250 	case Pmem:
251 		return (procfs_domem(curp, p, pfs, uio));
252 
253 	case Pcmdline:
254 		return (procfs_docmdline(curp, p, pfs, uio));
255 
256 	case Pmeminfo:
257 		return (procfs_domeminfo(curp, p, pfs, uio));
258 	case Pcpuinfo:
259 		return (procfs_docpuinfo(curp, p, pfs, uio));
260 
261 	default:
262 		return (EOPNOTSUPP);
263 	}
264 }
265 
266 /*
267  * Get a string from userland into (buf).  Strip a trailing
268  * nl character (to allow easy access from the shell).
269  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
270  * will automatically add a nul char at the end.
271  *
272  * Returns 0 on success or the following errors
273  *
274  * EINVAL:    file offset is non-zero.
275  * EMSGSIZE:  message is longer than kernel buffer
276  * EFAULT:    user i/o buffer is not addressable
277  */
278 int
279 vfs_getuserstr(uio, buf, buflenp)
280 	struct uio *uio;
281 	char *buf;
282 	int *buflenp;
283 {
284 	int xlen;
285 	int error;
286 
287 	if (uio->uio_offset != 0)
288 		return (EINVAL);
289 
290 	xlen = *buflenp;
291 
292 	/* must be able to read the whole string in one go */
293 	if (xlen < uio->uio_resid)
294 		return (EMSGSIZE);
295 	xlen = uio->uio_resid;
296 
297 	if ((error = uiomove(buf, xlen, uio)) != 0)
298 		return (error);
299 
300 	/* allow multiple writes without seeks */
301 	uio->uio_offset = 0;
302 
303 	/* cleanup string and remove trailing newline */
304 	buf[xlen] = '\0';
305 	xlen = strlen(buf);
306 	if (xlen > 0 && buf[xlen-1] == '\n')
307 		buf[--xlen] = '\0';
308 	*buflenp = xlen;
309 
310 	return (0);
311 }
312 
313 const vfs_namemap_t *
314 vfs_findname(nm, buf, buflen)
315 	const vfs_namemap_t *nm;
316 	const char *buf;
317 	int buflen;
318 {
319 
320 	for (; nm->nm_name; nm++)
321 		if (memcmp(buf, nm->nm_name, buflen+1) == 0)
322 			return (nm);
323 
324 	return (0);
325 }
326 
327 /*
328  * Initialize pfsnode hash table.
329  */
330 void
331 procfs_hashinit()
332 {
333 	lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
334 	pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
335 	    M_WAITOK, &pfs_ihash);
336 	simple_lock_init(&pfs_hash_slock);
337 }
338 
339 void
340 procfs_hashreinit()
341 {
342 	struct pfsnode *pp;
343 	struct pfs_hashhead *oldhash, *hash;
344 	u_long oldmask, mask, val;
345 	int i;
346 
347 	hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
348 	    &mask);
349 
350 	simple_lock(&pfs_hash_slock);
351 	oldhash = pfs_hashtbl;
352 	oldmask = pfs_ihash;
353 	pfs_hashtbl = hash;
354 	pfs_ihash = mask;
355 	for (i = 0; i <= oldmask; i++) {
356 		while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
357 			LIST_REMOVE(pp, pfs_hash);
358 			val = PFSPIDHASH(pp->pfs_pid);
359 			LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
360 		}
361 	}
362 	simple_unlock(&pfs_hash_slock);
363 	hashdone(oldhash, M_UFSMNT);
364 }
365 
366 /*
367  * Free pfsnode hash table.
368  */
369 void
370 procfs_hashdone()
371 {
372 	hashdone(pfs_hashtbl, M_UFSMNT);
373 }
374 
375 struct vnode *
376 procfs_hashget(pid, type, mp)
377 	pid_t pid;
378 	pfstype type;
379 	struct mount *mp;
380 {
381 	struct pfs_hashhead *ppp;
382 	struct pfsnode *pp;
383 	struct vnode *vp;
384 
385 loop:
386 	simple_lock(&pfs_hash_slock);
387 	ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
388 	LIST_FOREACH(pp, ppp, pfs_hash) {
389 		vp = PFSTOV(pp);
390 		if (pid == pp->pfs_pid && pp->pfs_type == type &&
391 		    vp->v_mount == mp) {
392 			simple_lock(&vp->v_interlock);
393 			simple_unlock(&pfs_hash_slock);
394 			if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
395 				goto loop;
396 			return (vp);
397 		}
398 	}
399 	simple_unlock(&pfs_hash_slock);
400 	return (NULL);
401 }
402 
403 /*
404  * Insert the pfsnode into the hash table and lock it.
405  */
406 void
407 procfs_hashins(pp)
408 	struct pfsnode *pp;
409 {
410 	struct pfs_hashhead *ppp;
411 
412 	/* lock the pfsnode, then put it on the appropriate hash list */
413 	lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
414 
415 	simple_lock(&pfs_hash_slock);
416 	ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
417 	LIST_INSERT_HEAD(ppp, pp, pfs_hash);
418 	simple_unlock(&pfs_hash_slock);
419 }
420 
421 /*
422  * Remove the pfsnode from the hash table.
423  */
424 void
425 procfs_hashrem(pp)
426 	struct pfsnode *pp;
427 {
428 	simple_lock(&pfs_hash_slock);
429 	LIST_REMOVE(pp, pfs_hash);
430 	simple_unlock(&pfs_hash_slock);
431 }
432 
433 void
434 procfs_revoke_vnodes(p, arg)
435 	struct proc *p;
436 	void *arg;
437 {
438 	struct pfsnode *pfs, *pnext;
439 	struct vnode *vp;
440 	struct mount *mp = (struct mount *)arg;
441 	struct pfs_hashhead *ppp;
442 
443 	if (!(p->p_flag & P_SUGID))
444 		return;
445 
446 	ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
447 	for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
448 		vp = PFSTOV(pfs);
449 		pnext = LIST_NEXT(pfs, pfs_hash);
450 		if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
451 		    vp->v_mount == mp)
452 			VOP_REVOKE(vp, REVOKEALL);
453 	}
454 }
455