2 * Copyright (c) 1993 Jan-Simon Pendry
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
35 * $FreeBSD: src/sys/miscfs/procfs/procfs_subr.c,v 1.26.2.3 2002/02/18 21:28:04 des Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sysctl.h>
43 #include <sys/mount.h>
44 #include <sys/vnode.h>
45 #include <sys/malloc.h>
46 #include <sys/spinlock.h>
48 #include <sys/spinlock2.h>
50 #include <vfs/procfs/procfs.h>
52 #define PFS_HSIZE 1031
56 struct pfsnode *first;
59 static struct pfshead pfshead[PFS_HSIZE];
60 static struct lock procfslk = LOCK_INITIALIZER("pvplk", 0, 0);
62 MALLOC_DEFINE(M_PROCFS, "procfs", "procfs v_data");
64 #define PFSHASH(pid) &pfshead[((pid) & ~PFS_DEAD) % PFS_HSIZE]
67 * Allocate a pfsnode/vnode pair. If no error occurs the returned vnode
68 * will be referenced and exclusively locked.
70 * The pid, pfs_type, and mount point uniquely identify a pfsnode.
71 * The mount point is needed because someone might mount this filesystem
74 * All pfsnodes are maintained on a singly-linked list. new nodes are
75 * only allocated when they cannot be found on this list. entries on
76 * the list are removed when the vfs reclaim entry is called.
78 * A single lock is kept for the entire list. this is needed because the
79 * getnewvnode() function can block waiting for a vnode to become free,
80 * in which case there may be more than one process trying to get the same
81 * vnode. this lock is only taken if we are going to call getnewvnode,
82 * since the kernel itself is single-threaded.
84 * If an entry is found on the list, then call vget() to take a reference
85 * and obtain the lock. This will properly re-reference the vnode if it
86 * had gotten onto the free list.
89 procfs_allocvp(struct mount *mp, struct vnode **vpp, long pid, pfstype pfs_type)
99 for (pfs = ph->first; pfs; pfs = pfs->pfs_next) {
100 if (pfs->pfs_pid == pid && pfs->pfs_type == pfs_type &&
101 PFSTOV(pfs)->v_mount == mp) {
104 spin_unlock(&ph->spin);
105 if (vget(vp, LK_EXCLUSIVE)) {
112 * Make sure the vnode is still in the cache after
113 * getting the interlock to avoid racing a free.
115 spin_lock(&ph->spin);
116 for (pfs = ph->first; pfs; pfs = pfs->pfs_next) {
117 if (PFSTOV(pfs) == vp &&
118 pfs->pfs_pid == pid &&
119 pfs->pfs_type == pfs_type &&
120 PFSTOV(pfs)->v_mount == mp) {
124 if (pfs == NULL || PFSTOV(pfs) != vp) {
125 spin_unlock(&ph->spin);
130 spin_unlock(&ph->spin);
135 spin_unlock(&ph->spin);
138 * otherwise lock the vp list while we call getnewvnode
139 * since that can block.
141 if (lockmgr(&procfslk, LK_EXCLUSIVE|LK_SLEEPFAIL))
145 * Do the MALLOC before the getnewvnode since doing so afterward
146 * might cause a bogus v_data pointer to get dereferenced
147 * elsewhere if MALLOC should block.
149 * XXX this may not matter anymore since getnewvnode now returns
152 pfs = kmalloc(sizeof(struct pfsnode), M_PROCFS, M_WAITOK);
154 error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0);
156 kfree(pfs, M_PROCFS);
164 pfs->pfs_pid = (pid_t) pid;
165 pfs->pfs_type = pfs_type;
168 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
169 lockinit(&pfs->pfs_lock, "pfslk", 0, 0);
172 case Proot: /* /proc = dr-xr-xr-x */
173 pfs->pfs_mode = (VREAD|VEXEC) |
180 case Pcurproc: /* /proc/curproc = lr--r--r-- */
181 pfs->pfs_mode = (VREAD) |
188 pfs->pfs_mode = (VREAD|VEXEC) |
195 pfs->pfs_mode = (VREAD|VEXEC) |
202 pfs->pfs_mode = (VREAD|VWRITE);
209 pfs->pfs_mode = (VREAD|VWRITE);
216 pfs->pfs_mode = (VWRITE);
225 pfs->pfs_mode = (VREAD) |
232 panic("procfs_allocvp");
235 /* add to procfs vnode list */
236 spin_lock(&ph->spin);
237 pfs->pfs_next = ph->first;
239 spin_unlock(&ph->spin);
243 lockmgr(&procfslk, LK_RELEASE);
249 procfs_freevp(struct vnode *vp)
257 ph = PFSHASH(pfs->pfs_pid);
259 spin_lock(&ph->spin);
262 KKASSERT(*pp != NULL);
263 pp = &(*pp)->pfs_next;
266 spin_unlock(&ph->spin);
268 pfs->pfs_next = NULL;
269 pfs->pfs_vnode = NULL;
270 kfree(pfs, M_PROCFS);
276 * Try to find the calling pid. Note that pfind()
277 * now references the proc structure to be returned
278 * and needs to be released later with PRELE().
281 pfs_pfind(pid_t pfs_pid)
283 struct proc *p = NULL;
293 * Make sure the process is not in the middle of exiting (where
294 * a lot of its structural members may wind up being NULL). If it
295 * is we give up on it.
298 lwkt_gettoken(&p->p_token);
299 if (p->p_flags & P_POSTEXIT) {
300 lwkt_reltoken(&p->p_token);
309 pfs_zpfind(pid_t pfs_pid)
311 struct proc *p = NULL;
321 * Make sure the process is not in the middle of exiting (where
322 * a lot of its structural members may wind up being NULL). If it
323 * is we give up on it.
326 lwkt_gettoken(&p->p_token);
327 if (p->p_flags & P_POSTEXIT) {
328 lwkt_reltoken(&p->p_token);
337 pfs_pdone(struct proc *p)
340 lwkt_reltoken(&p->p_token);
346 procfs_rw(struct vop_read_args *ap)
348 struct vnode *vp = ap->a_vp;
349 struct uio *uio = ap->a_uio;
350 struct thread *curtd = uio->uio_td;
352 struct pfsnode *pfs = VTOPFS(vp);
359 if ((curp = curtd->td_proc) == NULL) /* XXX */
362 p = pfs_pfind(pfs->pfs_pid);
367 if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE) {
375 lp = FIRST_LWP_IN_PROC(p);
382 lockmgr(&pfs->pfs_lock, LK_EXCLUSIVE);
384 switch (pfs->pfs_type) {
387 rtval = procfs_donote(curp, lp, pfs, uio);
391 rtval = procfs_doregs(curp, lp, pfs, uio);
395 rtval = procfs_dofpregs(curp, lp, pfs, uio);
399 rtval = procfs_dodbregs(curp, lp, pfs, uio);
403 rtval = procfs_doctl(curp, lp, pfs, uio);
407 rtval = procfs_dostatus(curp, lp, pfs, uio);
411 rtval = procfs_domap(curp, lp, pfs, uio);
415 rtval = procfs_domem(curp, lp, pfs, uio);
419 rtval = procfs_dotype(curp, lp, pfs, uio);
423 rtval = procfs_docmdline(curp, lp, pfs, uio);
427 rtval = procfs_dorlimit(curp, lp, pfs, uio);
436 lockmgr(&pfs->pfs_lock, LK_RELEASE);
444 * Get a string from userland into (buf). Strip a trailing
445 * nl character (to allow easy access from the shell).
446 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
447 * will automatically add a nul char at the end.
449 * Returns 0 on success or the following errors
451 * EINVAL: file offset is non-zero.
452 * EMSGSIZE: message is longer than kernel buffer
453 * EFAULT: user i/o buffer is not addressable
456 vfs_getuserstr(struct uio *uio, char *buf, int *buflenp)
461 if (uio->uio_offset != 0)
466 /* must be able to read the whole string in one go */
467 if (xlen < uio->uio_resid)
469 xlen = uio->uio_resid;
471 if ((error = uiomove(buf, xlen, uio)) != 0)
474 /* allow multiple writes without seeks */
477 /* cleanup string and remove trailing newline */
480 if (xlen > 0 && buf[xlen-1] == '\n')
488 vfs_findname(vfs_namemap_t *nm, char *buf, int buflen)
491 for (; nm->nm_name; nm++)
492 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
499 procfs_exit(struct thread *td)
506 KKASSERT(td->td_proc);
507 pid = td->td_proc->p_pid;
510 * NOTE: We can't just vgone() the vnode any more, not while
511 * it may potentially still be active. This will clean
512 * the vp and clear the mount and cause the new VOP subsystem
513 * to assert or panic when someone tries to do an operation
514 * on an open (exited) procfs descriptor.
516 * Prevent further operations on this pid by setting pfs_pid to -1.
517 * Note that a pfs_pid of 0 is used for nodes which do not track
518 * any particular pid.
520 * Use vx_get() to properly ref/lock a vp which may not have any
521 * refs and which may or may not already be reclaimed. vx_put()
522 * will then properly deactivate it and cause it to be recycled.
524 * The hash table can also get ripped out from under us when
525 * we block so take the easy way out and restart the scan.
529 spin_lock(&ph->spin);
530 for (pfs = ph->first; pfs; pfs = pfs->pfs_next) {
531 if (pfs->pfs_pid == pid)
535 spin_unlock(&ph->spin);
540 spin_unlock(&ph->spin);
542 pfs->pfs_pid |= PFS_DEAD; /* does not effect hash */