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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
39 * $FreeBSD: src/sys/miscfs/procfs/procfs_subr.c,v 1.26.2.3 2002/02/18 21:28:04 des Exp $
40 * $DragonFly: src/sys/vfs/procfs/procfs_subr.c,v 1.18 2007/08/25 23:27:02 corecode Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysctl.h>
47 #include <sys/mount.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include <sys/thread2.h>
52 #include <vfs/procfs/procfs.h>
55 #define PFS_HMASK (PFS_HSIZE - 1)
57 static struct pfsnode *pfshead[PFS_HSIZE];
60 #define PFSHASH(pid) &pfshead[(pid) & PFS_HMASK]
63 * Allocate a pfsnode/vnode pair. If no error occurs the returned vnode
64 * will be referenced and exclusively locked.
66 * The pid, pfs_type, and mount point uniquely identify a pfsnode.
67 * The mount point is needed because someone might mount this filesystem
70 * All pfsnodes are maintained on a singly-linked list. new nodes are
71 * only allocated when they cannot be found on this list. entries on
72 * the list are removed when the vfs reclaim entry is called.
74 * A single lock is kept for the entire list. this is needed because the
75 * getnewvnode() function can block waiting for a vnode to become free,
76 * in which case there may be more than one process trying to get the same
77 * vnode. this lock is only taken if we are going to call getnewvnode,
78 * since the kernel itself is single-threaded.
80 * If an entry is found on the list, then call vget() to take a reference
81 * and obtain the lock. This will properly re-reference the vnode if it
82 * had gotten onto the free list.
85 procfs_allocvp(struct mount *mp, struct vnode **vpp, long pid, pfstype pfs_type)
94 for (pfs = *pp; pfs; pfs = pfs->pfs_next) {
95 if (pfs->pfs_pid == pid && pfs->pfs_type == pfs_type &&
96 PFSTOV(pfs)->v_mount == mp) {
98 if (vget(vp, LK_EXCLUSIVE))
102 * Make sure the vnode is still in the cache after
103 * getting the interlock to avoid racing a free.
105 for (pfs = *pp; pfs; pfs = pfs->pfs_next) {
106 if (PFSTOV(pfs) == vp &&
107 pfs->pfs_pid == pid &&
108 pfs->pfs_type == pfs_type &&
109 PFSTOV(pfs)->v_mount == mp) {
113 if (pfs == NULL || PFSTOV(pfs) != vp) {
124 * otherwise lock the vp list while we call getnewvnode
125 * since that can block.
127 if (pfsvplock & PROCFS_LOCKED) {
128 pfsvplock |= PROCFS_WANT;
129 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
132 pfsvplock |= PROCFS_LOCKED;
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.
139 * XXX this may not matter anymore since getnewvnode now returns
142 MALLOC(pfs, struct pfsnode *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
144 error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0);
154 pfs->pfs_pid = (pid_t) pid;
155 pfs->pfs_type = pfs_type;
158 pfs->pfs_lockowner = 0;
159 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
162 case Proot: /* /proc = dr-xr-xr-x */
163 pfs->pfs_mode = (VREAD|VEXEC) |
170 case Pcurproc: /* /proc/curproc = lr--r--r-- */
171 pfs->pfs_mode = (VREAD) |
178 pfs->pfs_mode = (VREAD|VEXEC) |
185 pfs->pfs_mode = (VREAD|VEXEC) |
192 pfs->pfs_mode = (VREAD|VWRITE);
199 pfs->pfs_mode = (VREAD|VWRITE);
206 pfs->pfs_mode = (VWRITE);
215 pfs->pfs_mode = (VREAD) |
222 panic("procfs_allocvp");
225 /* add to procfs vnode list */
230 pfsvplock &= ~PROCFS_LOCKED;
232 if (pfsvplock & PROCFS_WANT) {
233 pfsvplock &= ~PROCFS_WANT;
234 wakeup((caddr_t) &pfsvplock);
241 procfs_freevp(struct vnode *vp)
243 struct pfsnode **pfspp;
249 pfspp = PFSHASH(pfs->pfs_pid);
250 while (*pfspp != pfs && *pfspp)
251 pfspp = &(*pfspp)->pfs_next;
253 *pfspp = pfs->pfs_next;
254 pfs->pfs_next = NULL;
260 * Try to find the calling pid. Note that pfind()
261 * now references the proc structure to be returned
262 * and needs to be released later with PRELE().
265 pfs_pfind(pid_t pfs_pid)
267 struct proc *p = NULL;
280 procfs_rw(struct vop_read_args *ap)
282 struct vnode *vp = ap->a_vp;
283 struct uio *uio = ap->a_uio;
284 struct thread *curtd = uio->uio_td;
286 struct pfsnode *pfs = VTOPFS(vp);
293 if ((curp = curtd->td_proc) == NULL) /* XXX */
296 lwkt_gettoken(&proc_token);
298 p = pfs_pfind(pfs->pfs_pid);
303 if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE) {
308 lp = FIRST_LWP_IN_PROC(p);
311 while (pfs->pfs_lockowner) {
312 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
314 pfs->pfs_lockowner = curproc->p_pid;
316 lwkt_gettoken(&p->p_token);
318 switch (pfs->pfs_type) {
321 rtval = procfs_donote(curp, lp, pfs, uio);
325 rtval = procfs_doregs(curp, lp, pfs, uio);
329 rtval = procfs_dofpregs(curp, lp, pfs, uio);
333 rtval = procfs_dodbregs(curp, lp, pfs, uio);
337 rtval = procfs_doctl(curp, lp, pfs, uio);
341 rtval = procfs_dostatus(curp, lp, pfs, uio);
345 rtval = procfs_domap(curp, lp, pfs, uio);
349 rtval = procfs_domem(curp, lp, pfs, uio);
353 rtval = procfs_dotype(curp, lp, pfs, uio);
357 rtval = procfs_docmdline(curp, lp, pfs, uio);
361 rtval = procfs_dorlimit(curp, lp, pfs, uio);
368 lwkt_reltoken(&p->p_token);
371 pfs->pfs_lockowner = 0;
372 lwkt_reltoken(&proc_token);
373 wakeup(&pfs->pfs_lockowner);
376 if (LWKT_TOKEN_HELD(&proc_token))
377 lwkt_reltoken(&proc_token);
385 * Get a string from userland into (buf). Strip a trailing
386 * nl character (to allow easy access from the shell).
387 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
388 * will automatically add a nul char at the end.
390 * Returns 0 on success or the following errors
392 * EINVAL: file offset is non-zero.
393 * EMSGSIZE: message is longer than kernel buffer
394 * EFAULT: user i/o buffer is not addressable
397 vfs_getuserstr(struct uio *uio, char *buf, int *buflenp)
402 if (uio->uio_offset != 0)
407 /* must be able to read the whole string in one go */
408 if (xlen < uio->uio_resid)
410 xlen = uio->uio_resid;
412 if ((error = uiomove(buf, xlen, uio)) != 0)
415 /* allow multiple writes without seeks */
418 /* cleanup string and remove trailing newline */
421 if (xlen > 0 && buf[xlen-1] == '\n')
429 vfs_findname(vfs_namemap_t *nm, char *buf, int buflen)
432 for (; nm->nm_name; nm++)
433 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
440 procfs_exit(struct thread *td)
446 KKASSERT(td->td_proc);
447 pid = td->td_proc->p_pid;
450 * NOTE: We can't just vgone() the vnode any more, not while
451 * it may potentially still be active. This will clean
452 * the vp and clear the mount and cause the new VOP subsystem
453 * to assert or panic when someone tries to do an operation
454 * on an open (exited) procfs descriptor.
456 * Prevent further operations on this pid by setting pfs_pid to -1.
457 * Note that a pfs_pid of 0 is used for nodes which do not track
458 * any particular pid.
460 * Use vx_get() to properly ref/lock a vp which may not have any
461 * refs and which may or may not already be reclaimed. vx_put()
462 * will then properly deactivate it and cause it to be recycled.
464 * The hash table can also get ripped out from under us when
465 * we block so take the easy way out and restart the scan.
470 if (pfs->pfs_pid == pid) {
473 pfs->pfs_pid |= PFS_DEAD; /* does not effect hash */