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>
42 #include <sys/mount.h>
43 #include <sys/vnode.h>
44 #include <sys/malloc.h>
45 #include <sys/spinlock.h>
47 #include <sys/spinlock2.h>
49 #include <vfs/procfs/procfs.h>
51 #define PFS_HSIZE 1031
55 struct pfsnode *first;
58 static struct pfshead pfshead[PFS_HSIZE];
59 static struct lock procfslk = LOCK_INITIALIZER("pvplk", 0, 0);
61 MALLOC_DEFINE(M_PROCFS, "procfs", "procfs v_data");
63 #define PFSHASH(pid) &pfshead[((pid) & ~PFS_DEAD) % PFS_HSIZE]
66 * Allocate a pfsnode/vnode pair. If no error occurs the returned vnode
67 * will be referenced and exclusively locked.
69 * The pid, pfs_type, and mount point uniquely identify a pfsnode.
70 * The mount point is needed because someone might mount this filesystem
73 * All pfsnodes are maintained on a singly-linked list. new nodes are
74 * only allocated when they cannot be found on this list. entries on
75 * the list are removed when the vfs reclaim entry is called.
77 * A single lock is kept for the entire list. this is needed because the
78 * getnewvnode() function can block waiting for a vnode to become free,
79 * in which case there may be more than one process trying to get the same
80 * vnode. this lock is only taken if we are going to call getnewvnode,
81 * since the kernel itself is single-threaded.
83 * If an entry is found on the list, then call vget() to take a reference
84 * and obtain the lock. This will properly re-reference the vnode if it
85 * had gotten onto the free list.
88 procfs_allocvp(struct mount *mp, struct vnode **vpp, long pid, pfstype pfs_type)
98 for (pfs = ph->first; pfs; pfs = pfs->pfs_next) {
99 if (pfs->pfs_pid == pid && pfs->pfs_type == pfs_type &&
100 PFSTOV(pfs)->v_mount == mp) {
103 spin_unlock(&ph->spin);
104 if (vget(vp, LK_EXCLUSIVE)) {
111 * Make sure the vnode is still in the cache after
112 * getting the interlock to avoid racing a free.
114 spin_lock(&ph->spin);
115 for (pfs = ph->first; pfs; pfs = pfs->pfs_next) {
116 if (PFSTOV(pfs) == vp &&
117 pfs->pfs_pid == pid &&
118 pfs->pfs_type == pfs_type &&
119 PFSTOV(pfs)->v_mount == mp) {
123 if (pfs == NULL || PFSTOV(pfs) != vp) {
124 spin_unlock(&ph->spin);
129 spin_unlock(&ph->spin);
134 spin_unlock(&ph->spin);
137 * otherwise lock the vp list while we call getnewvnode
138 * since that can block.
140 if (lockmgr(&procfslk, LK_EXCLUSIVE|LK_SLEEPFAIL))
144 * Do the MALLOC before the getnewvnode since doing so afterward
145 * might cause a bogus v_data pointer to get dereferenced
146 * elsewhere if MALLOC should block.
148 * XXX this may not matter anymore since getnewvnode now returns
151 pfs = kmalloc(sizeof(struct pfsnode), M_PROCFS, M_WAITOK);
153 error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0);
155 kfree(pfs, M_PROCFS);
163 pfs->pfs_pid = (pid_t) pid;
164 pfs->pfs_type = pfs_type;
167 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
168 lockinit(&pfs->pfs_lock, "pfslk", 0, 0);
171 case Proot: /* /proc = dr-xr-xr-x */
172 pfs->pfs_mode = (VREAD|VEXEC) |
179 case Pcurproc: /* /proc/curproc = lr--r--r-- */
180 pfs->pfs_mode = (VREAD) |
187 pfs->pfs_mode = (VREAD|VEXEC) |
194 pfs->pfs_mode = (VREAD|VEXEC) |
201 pfs->pfs_mode = (VREAD|VWRITE);
208 pfs->pfs_mode = (VREAD|VWRITE);
215 pfs->pfs_mode = (VWRITE);
224 pfs->pfs_mode = (VREAD) |
231 panic("procfs_allocvp");
234 /* add to procfs vnode list */
235 spin_lock(&ph->spin);
236 pfs->pfs_next = ph->first;
238 spin_unlock(&ph->spin);
241 lockmgr(&procfslk, LK_RELEASE);
247 procfs_freevp(struct vnode *vp)
255 ph = PFSHASH(pfs->pfs_pid);
257 spin_lock(&ph->spin);
260 KKASSERT(*pp != NULL);
261 pp = &(*pp)->pfs_next;
264 spin_unlock(&ph->spin);
266 pfs->pfs_next = NULL;
267 pfs->pfs_vnode = NULL;
268 kfree(pfs, M_PROCFS);
274 * Try to find the calling pid. Note that pfind()
275 * now references the proc structure to be returned
276 * and needs to be released later with PRELE().
279 pfs_pfind(pid_t pfs_pid)
281 struct proc *p = NULL;
291 * Make sure the process is not in the middle of exiting (where
292 * a lot of its structural members may wind up being NULL). If it
293 * is we give up on it.
296 lwkt_gettoken(&p->p_token);
297 if (p->p_flags & P_POSTEXIT) {
298 lwkt_reltoken(&p->p_token);
307 pfs_zpfind(pid_t pfs_pid)
309 struct proc *p = NULL;
319 * Make sure the process is not in the middle of exiting (where
320 * a lot of its structural members may wind up being NULL). If it
321 * is we give up on it.
324 lwkt_gettoken(&p->p_token);
325 if (p->p_flags & P_POSTEXIT) {
326 lwkt_reltoken(&p->p_token);
335 pfs_pdone(struct proc *p)
338 lwkt_reltoken(&p->p_token);
344 procfs_rw(struct vop_read_args *ap)
346 struct vnode *vp = ap->a_vp;
347 struct uio *uio = ap->a_uio;
348 struct thread *curtd = uio->uio_td;
350 struct pfsnode *pfs = VTOPFS(vp);
357 if ((curp = curtd->td_proc) == NULL) /* XXX */
360 p = pfs_pfind(pfs->pfs_pid);
365 if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE) {
370 lp = FIRST_LWP_IN_PROC(p);
373 lockmgr(&pfs->pfs_lock, LK_EXCLUSIVE);
375 switch (pfs->pfs_type) {
378 rtval = procfs_donote(curp, lp, pfs, uio);
382 rtval = procfs_doregs(curp, lp, pfs, uio);
386 rtval = procfs_dofpregs(curp, lp, pfs, uio);
390 rtval = procfs_dodbregs(curp, lp, pfs, uio);
394 rtval = procfs_doctl(curp, lp, pfs, uio);
398 rtval = procfs_dostatus(curp, lp, pfs, uio);
402 rtval = procfs_domap(curp, lp, pfs, uio);
406 rtval = procfs_domem(curp, lp, pfs, uio);
410 rtval = procfs_dotype(curp, lp, pfs, uio);
414 rtval = procfs_docmdline(curp, lp, pfs, uio);
418 rtval = procfs_dorlimit(curp, lp, pfs, uio);
427 lockmgr(&pfs->pfs_lock, LK_RELEASE);
435 * Get a string from userland into (buf). Strip a trailing
436 * nl character (to allow easy access from the shell).
437 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
438 * will automatically add a nul char at the end.
440 * Returns 0 on success or the following errors
442 * EINVAL: file offset is non-zero.
443 * EMSGSIZE: message is longer than kernel buffer
444 * EFAULT: user i/o buffer is not addressable
447 vfs_getuserstr(struct uio *uio, char *buf, int *buflenp)
452 if (uio->uio_offset != 0)
457 /* must be able to read the whole string in one go */
458 if (xlen < uio->uio_resid)
460 xlen = uio->uio_resid;
462 if ((error = uiomove(buf, xlen, uio)) != 0)
465 /* allow multiple writes without seeks */
468 /* cleanup string and remove trailing newline */
471 if (xlen > 0 && buf[xlen-1] == '\n')
479 vfs_findname(vfs_namemap_t *nm, char *buf, int buflen)
482 for (; nm->nm_name; nm++)
483 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
490 procfs_exit(struct thread *td)
497 KKASSERT(td->td_proc);
498 pid = td->td_proc->p_pid;
501 * NOTE: We can't just vgone() the vnode any more, not while
502 * it may potentially still be active. This will clean
503 * the vp and clear the mount and cause the new VOP subsystem
504 * to assert or panic when someone tries to do an operation
505 * on an open (exited) procfs descriptor.
507 * Prevent further operations on this pid by setting pfs_pid to -1.
508 * Note that a pfs_pid of 0 is used for nodes which do not track
509 * any particular pid.
511 * Use vx_get() to properly ref/lock a vp which may not have any
512 * refs and which may or may not already be reclaimed. vx_put()
513 * will then properly deactivate it and cause it to be recycled.
515 * The hash table can also get ripped out from under us when
516 * we block so take the easy way out and restart the scan.
520 spin_lock(&ph->spin);
521 for (pfs = ph->first; pfs; pfs = pfs->pfs_next) {
522 if (pfs->pfs_pid == pid)
526 spin_unlock(&ph->spin);
531 spin_unlock(&ph->spin);
533 pfs->pfs_pid |= PFS_DEAD; /* does not effect hash */