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