xref: /dragonfly/sys/vfs/procfs/procfs_subr.c (revision e65bc1c3)
1 /*
2  * Copyright (c) 1993 Jan-Simon Pendry
3  * Copyright (c) 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)procfs_subr.c	8.6 (Berkeley) 5/14/95
34  *
35  * $FreeBSD: src/sys/miscfs/procfs/procfs_subr.c,v 1.26.2.3 2002/02/18 21:28:04 des Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysctl.h>
41 #include <sys/proc.h>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
45 #include <sys/thread2.h>
46 
47 #include <vfs/procfs/procfs.h>
48 
49 #define PFS_HSIZE	256
50 #define PFS_HMASK	(PFS_HSIZE - 1)
51 
52 static struct pfsnode *pfshead[PFS_HSIZE];
53 static int pfsvplock;
54 
55 #define PFSHASH(pid)	&pfshead[(pid) & PFS_HMASK]
56 
57 /*
58  * Allocate a pfsnode/vnode pair.  If no error occurs the returned vnode
59  * will be referenced and exclusively locked.
60  *
61  * The pid, pfs_type, and mount point uniquely identify a pfsnode.
62  * The mount point is needed because someone might mount this filesystem
63  * twice.
64  *
65  * All pfsnodes are maintained on a singly-linked list.  new nodes are
66  * only allocated when they cannot be found on this list.  entries on
67  * the list are removed when the vfs reclaim entry is called.
68  *
69  * A single lock is kept for the entire list.  this is needed because the
70  * getnewvnode() function can block waiting for a vnode to become free,
71  * in which case there may be more than one process trying to get the same
72  * vnode.  this lock is only taken if we are going to call getnewvnode,
73  * since the kernel itself is single-threaded.
74  *
75  * If an entry is found on the list, then call vget() to take a reference
76  * and obtain the lock.  This will properly re-reference the vnode if it
77  * had gotten onto the free list.
78  */
79 int
80 procfs_allocvp(struct mount *mp, struct vnode **vpp, long pid, pfstype pfs_type)
81 {
82 	struct pfsnode *pfs;
83 	struct vnode *vp;
84 	struct pfsnode **pp;
85 	int error;
86 
87 	pp = PFSHASH(pid);
88 loop:
89 	for (pfs = *pp; pfs; pfs = pfs->pfs_next) {
90 		if (pfs->pfs_pid == pid && pfs->pfs_type == pfs_type &&
91 		    PFSTOV(pfs)->v_mount == mp) {
92 			vp = PFSTOV(pfs);
93 			vhold_interlocked(vp);
94 			if (vget(vp, LK_EXCLUSIVE)) {
95 				vdrop(vp);
96 				goto loop;
97 			}
98 
99 			/*
100 			 * Make sure the vnode is still in the cache after
101 			 * getting the interlock to avoid racing a free.
102 			 */
103 			for (pfs = *pp; pfs; pfs = pfs->pfs_next) {
104 				if (PFSTOV(pfs) == vp &&
105 				    pfs->pfs_pid == pid &&
106 				    pfs->pfs_type == pfs_type &&
107 				    PFSTOV(pfs)->v_mount == mp) {
108 					break;
109 				}
110 			}
111 			vdrop(vp);
112 			if (pfs == NULL || PFSTOV(pfs) != vp) {
113 				vput(vp);
114 				goto loop;
115 
116 			}
117 			KKASSERT(vp->v_data == pfs);
118 			*vpp = vp;
119 			return (0);
120 		}
121 	}
122 
123 	/*
124 	 * otherwise lock the vp list while we call getnewvnode
125 	 * since that can block.
126 	 */
127 	if (pfsvplock & PROCFS_LOCKED) {
128 		pfsvplock |= PROCFS_WANT;
129 		(void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
130 		goto loop;
131 	}
132 	pfsvplock |= PROCFS_LOCKED;
133 
134 	/*
135 	 * Do the MALLOC before the getnewvnode since doing so afterward
136 	 * might cause a bogus v_data pointer to get dereferenced
137 	 * elsewhere if MALLOC should block.
138 	 *
139 	 * XXX this may not matter anymore since getnewvnode now returns
140 	 * a VX locked vnode.
141 	 */
142 	pfs = kmalloc(sizeof(struct pfsnode), M_TEMP, M_WAITOK);
143 
144 	error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0);
145 	if (error) {
146 		kfree(pfs, M_TEMP);
147 		goto out;
148 	}
149 	vp = *vpp;
150 
151 	vp->v_data = pfs;
152 
153 	pfs->pfs_next = 0;
154 	pfs->pfs_pid = (pid_t) pid;
155 	pfs->pfs_type = pfs_type;
156 	pfs->pfs_vnode = vp;
157 	pfs->pfs_flags = 0;
158 	pfs->pfs_lockowner = 0;
159 	pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
160 
161 	switch (pfs_type) {
162 	case Proot:	/* /proc = dr-xr-xr-x */
163 		pfs->pfs_mode = (VREAD|VEXEC) |
164 				(VREAD|VEXEC) >> 3 |
165 				(VREAD|VEXEC) >> 6;
166 		vp->v_type = VDIR;
167 		vp->v_flag = VROOT;
168 		break;
169 
170 	case Pcurproc:	/* /proc/curproc = lr--r--r-- */
171 		pfs->pfs_mode = (VREAD) |
172 				(VREAD >> 3) |
173 				(VREAD >> 6);
174 		vp->v_type = VLNK;
175 		break;
176 
177 	case Pproc:
178 		pfs->pfs_mode = (VREAD|VEXEC) |
179 				(VREAD|VEXEC) >> 3 |
180 				(VREAD|VEXEC) >> 6;
181 		vp->v_type = VDIR;
182 		break;
183 
184 	case Pfile:
185 		pfs->pfs_mode = (VREAD|VEXEC) |
186 				(VREAD|VEXEC) >> 3 |
187 				(VREAD|VEXEC) >> 6;
188 		vp->v_type = VLNK;
189 		break;
190 
191 	case Pmem:
192 		pfs->pfs_mode = (VREAD|VWRITE);
193 		vp->v_type = VREG;
194 		break;
195 
196 	case Pregs:
197 	case Pfpregs:
198 	case Pdbregs:
199 		pfs->pfs_mode = (VREAD|VWRITE);
200 		vp->v_type = VREG;
201 		break;
202 
203 	case Pctl:
204 	case Pnote:
205 	case Pnotepg:
206 		pfs->pfs_mode = (VWRITE);
207 		vp->v_type = VREG;
208 		break;
209 
210 	case Ptype:
211 	case Pmap:
212 	case Pstatus:
213 	case Pcmdline:
214 	case Prlimit:
215 		pfs->pfs_mode = (VREAD) |
216 				(VREAD >> 3) |
217 				(VREAD >> 6);
218 		vp->v_type = VREG;
219 		break;
220 
221 	default:
222 		panic("procfs_allocvp");
223 	}
224 
225 	/* add to procfs vnode list */
226 	pfs->pfs_next = *pp;
227 	*pp = pfs;
228 
229 out:
230 	pfsvplock &= ~PROCFS_LOCKED;
231 
232 	if (pfsvplock & PROCFS_WANT) {
233 		pfsvplock &= ~PROCFS_WANT;
234 		wakeup((caddr_t) &pfsvplock);
235 	}
236 
237 	return (error);
238 }
239 
240 int
241 procfs_freevp(struct vnode *vp)
242 {
243 	struct pfsnode **pfspp;
244 	struct pfsnode *pfs;
245 
246 	pfs = VTOPFS(vp);
247 	vp->v_data = NULL;
248 
249 	pfspp = PFSHASH(pfs->pfs_pid);
250 	while (*pfspp != pfs && *pfspp)
251 		pfspp = &(*pfspp)->pfs_next;
252 	KKASSERT(*pfspp);
253 	*pfspp = pfs->pfs_next;
254 	pfs->pfs_next = NULL;
255 	pfs->pfs_vnode = NULL;
256 	kfree(pfs, M_TEMP);
257 	return (0);
258 }
259 
260 /*
261  * Try to find the calling pid. Note that pfind()
262  * now references the proc structure to be returned
263  * and needs to be released later with PRELE().
264  */
265 struct proc *
266 pfs_pfind(pid_t pfs_pid)
267 {
268 	struct proc *p = NULL;
269 
270 	if (pfs_pid == 0) {
271 		p = &proc0;
272 		PHOLD(p);
273 	} else {
274 		p = pfind(pfs_pid);
275 	}
276 
277 	/*
278 	 * Make sure the process is not in the middle of exiting (where
279 	 * a lot of its structural members may wind up being NULL).  If it
280 	 * is we give up on it.
281 	 */
282 	if (p) {
283 		lwkt_gettoken(&p->p_token);
284 		if (p->p_flags & P_POSTEXIT) {
285 			lwkt_reltoken(&p->p_token);
286 			PRELE(p);
287 			p = NULL;
288 		}
289 	}
290 	return p;
291 }
292 
293 struct proc *
294 pfs_zpfind(pid_t pfs_pid)
295 {
296 	struct proc *p = NULL;
297 
298 	if (pfs_pid == 0) {
299 		p = &proc0;
300 		PHOLD(p);
301 	} else {
302 		p = zpfind(pfs_pid);
303 	}
304 
305 	/*
306 	 * Make sure the process is not in the middle of exiting (where
307 	 * a lot of its structural members may wind up being NULL).  If it
308 	 * is we give up on it.
309 	 */
310 	if (p) {
311 		lwkt_gettoken(&p->p_token);
312 		if (p->p_flags & P_POSTEXIT) {
313 			lwkt_reltoken(&p->p_token);
314 			PRELE(p);
315 			p = NULL;
316 		}
317 	}
318 	return p;
319 }
320 
321 void
322 pfs_pdone(struct proc *p)
323 {
324 	if (p) {
325 		lwkt_reltoken(&p->p_token);
326 		PRELE(p);
327 	}
328 }
329 
330 int
331 procfs_rw(struct vop_read_args *ap)
332 {
333 	struct vnode *vp = ap->a_vp;
334 	struct uio *uio = ap->a_uio;
335 	struct thread *curtd = uio->uio_td;
336 	struct proc *curp;
337 	struct pfsnode *pfs = VTOPFS(vp);
338 	struct proc *p;
339 	struct lwp *lp;
340 	int rtval;
341 
342 	if (curtd == NULL)
343 		return (EINVAL);
344 	if ((curp = curtd->td_proc) == NULL)	/* XXX */
345 		return (EINVAL);
346 
347 	lwkt_gettoken(&proc_token);
348 	p = pfs_pfind(pfs->pfs_pid);
349 	if (p == NULL) {
350 		rtval = EINVAL;
351 		goto out;
352 	}
353 	if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE) {
354 		rtval = EACCES;
355 		goto out;
356 	}
357 	/* XXX lwp */
358 	lp = FIRST_LWP_IN_PROC(p);
359 	LWPHOLD(lp);
360 
361 	while (pfs->pfs_lockowner) {
362 		tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
363 	}
364 	pfs->pfs_lockowner = curproc->p_pid;
365 
366 	switch (pfs->pfs_type) {
367 	case Pnote:
368 	case Pnotepg:
369 		rtval = procfs_donote(curp, lp, pfs, uio);
370 		break;
371 
372 	case Pregs:
373 		rtval = procfs_doregs(curp, lp, pfs, uio);
374 		break;
375 
376 	case Pfpregs:
377 		rtval = procfs_dofpregs(curp, lp, pfs, uio);
378 		break;
379 
380         case Pdbregs:
381                 rtval = procfs_dodbregs(curp, lp, pfs, uio);
382                 break;
383 
384 	case Pctl:
385 		rtval = procfs_doctl(curp, lp, pfs, uio);
386 		break;
387 
388 	case Pstatus:
389 		rtval = procfs_dostatus(curp, lp, pfs, uio);
390 		break;
391 
392 	case Pmap:
393 		rtval = procfs_domap(curp, lp, pfs, uio);
394 		break;
395 
396 	case Pmem:
397 		rtval = procfs_domem(curp, lp, pfs, uio);
398 		break;
399 
400 	case Ptype:
401 		rtval = procfs_dotype(curp, lp, pfs, uio);
402 		break;
403 
404 	case Pcmdline:
405 		rtval = procfs_docmdline(curp, lp, pfs, uio);
406 		break;
407 
408 	case Prlimit:
409 		rtval = procfs_dorlimit(curp, lp, pfs, uio);
410 		break;
411 
412 	default:
413 		rtval = EOPNOTSUPP;
414 		break;
415 	}
416 	LWPRELE(lp);
417 
418 	pfs->pfs_lockowner = 0;
419 	wakeup(&pfs->pfs_lockowner);
420 
421 out:
422 	pfs_pdone(p);
423 	lwkt_reltoken(&proc_token);
424 
425 	return rtval;
426 }
427 
428 /*
429  * Get a string from userland into (buf).  Strip a trailing
430  * nl character (to allow easy access from the shell).
431  * The buffer should be *buflenp + 1 chars long.  vfs_getuserstr
432  * will automatically add a nul char at the end.
433  *
434  * Returns 0 on success or the following errors
435  *
436  * EINVAL:    file offset is non-zero.
437  * EMSGSIZE:  message is longer than kernel buffer
438  * EFAULT:    user i/o buffer is not addressable
439  */
440 int
441 vfs_getuserstr(struct uio *uio, char *buf, int *buflenp)
442 {
443 	int xlen;
444 	int error;
445 
446 	if (uio->uio_offset != 0)
447 		return (EINVAL);
448 
449 	xlen = *buflenp;
450 
451 	/* must be able to read the whole string in one go */
452 	if (xlen < uio->uio_resid)
453 		return (EMSGSIZE);
454 	xlen = uio->uio_resid;
455 
456 	if ((error = uiomove(buf, xlen, uio)) != 0)
457 		return (error);
458 
459 	/* allow multiple writes without seeks */
460 	uio->uio_offset = 0;
461 
462 	/* cleanup string and remove trailing newline */
463 	buf[xlen] = '\0';
464 	xlen = strlen(buf);
465 	if (xlen > 0 && buf[xlen-1] == '\n')
466 		buf[--xlen] = '\0';
467 	*buflenp = xlen;
468 
469 	return (0);
470 }
471 
472 vfs_namemap_t *
473 vfs_findname(vfs_namemap_t *nm, char *buf, int buflen)
474 {
475 
476 	for (; nm->nm_name; nm++)
477 		if (bcmp(buf, nm->nm_name, buflen+1) == 0)
478 			return (nm);
479 
480 	return (0);
481 }
482 
483 void
484 procfs_exit(struct thread *td)
485 {
486 	struct pfsnode *pfs;
487 	struct vnode *vp;
488 	pid_t pid;
489 
490 	KKASSERT(td->td_proc);
491 	pid = td->td_proc->p_pid;
492 
493 	/*
494 	 * NOTE: We can't just vgone() the vnode any more, not while
495 	 * 	 it may potentially still be active.  This will clean
496 	 *	 the vp and clear the mount and cause the new VOP subsystem
497 	 *	 to assert or panic when someone tries to do an operation
498 	 *	 on an open (exited) procfs descriptor.
499 	 *
500 	 * Prevent further operations on this pid by setting pfs_pid to -1.
501 	 * Note that a pfs_pid of 0 is used for nodes which do not track
502 	 * any particular pid.
503 	 *
504 	 * Use vx_get() to properly ref/lock a vp which may not have any
505 	 * refs and which may or may not already be reclaimed.  vx_put()
506 	 * will then properly deactivate it and cause it to be recycled.
507 	 *
508 	 * The hash table can also get ripped out from under us when
509 	 * we block so take the easy way out and restart the scan.
510 	 */
511 again:
512 	pfs = *PFSHASH(pid);
513 	while (pfs) {
514 		if (pfs->pfs_pid == pid) {
515 			vp = PFSTOV(pfs);
516 			vx_get(vp);
517 			pfs->pfs_pid |= PFS_DEAD; /* does not effect hash */
518 			vx_put(vp);
519 			goto again;
520 		}
521 		pfs = pfs->pfs_next;
522 	}
523 }
524 
525