2 * Copyright (c) 2000 Dag-Erling Coïdan Smørgrav
3 * Copyright (c) 1999 Pierre Beyssac
4 * Copyright (c) 1993 Jan-Simon Pendry
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
41 * $FreeBSD: src/sys/i386/linux/linprocfs/linprocfs_subr.c,v 1.3.2.4 2001/06/25 19:46:47 pirzyk Exp $
42 * $DragonFly: src/sys/emulation/linux/i386/linprocfs/linprocfs_subr.c,v 1.16 2005/12/10 16:06:20 swildner Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include "linprocfs.h"
54 #define PFSHMASK (PFSHSIZE - 1)
56 static struct pfsnode *pfshead[PFSHSIZE];
59 extern int procfs_domem (struct proc *, struct proc *, struct pfsnode *pfsp, struct uio *uio);
62 * allocate a pfsnode/vnode pair. the vnode is
63 * referenced, but not locked.
65 * the pid, pfs_type, and mount point uniquely
66 * identify a pfsnode. the mount point is needed
67 * because someone might mount this filesystem
70 * all pfsnodes are maintained on a singly-linked
71 * list. new nodes are only allocated when they cannot
72 * be found on this list. entries on the list are
73 * removed when the vfs reclaim entry is called.
75 * a single lock is kept for the entire list. this is
76 * needed because the getnewvnode() function can block
77 * waiting for a vnode to become free, in which case there
78 * may be more than one process trying to get the same
79 * vnode. this lock is only taken if we are going to
80 * call getnewvnode, since the kernel itself is single-threaded.
82 * if an entry is found on the list, then call vget() to
83 * take a reference. this is done because there may be
84 * zero references to it and so it needs to removed from
85 * the vnode free list.
88 linprocfs_allocvp(struct mount *mp, struct vnode **vpp, long pid,
91 struct thread *td = curthread; /* XXX */
98 for (pfs = pfshead[pid & PFSHMASK]; pfs; pfs = pfs->pfs_next) {
100 if (pfs->pfs_pid == pid &&
101 pfs->pfs_type == pfs_type &&
103 if (vget(vp, LK_EXCLUSIVE|LK_SLEEPFAIL, td))
111 * otherwise lock the vp list while we call getnewvnode
112 * since that can block.
114 if (pfsvplock & PROCFS_LOCKED) {
115 pfsvplock |= PROCFS_WANT;
116 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
119 pfsvplock |= PROCFS_LOCKED;
122 * Do the MALLOC before the getnewvnode since doing so afterward
123 * might cause a bogus v_data pointer to get dereferenced
124 * elsewhere if MALLOC should block.
126 MALLOC(pfs, struct pfsnode *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
128 error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0);
138 pfs->pfs_pid = (pid_t) pid;
139 pfs->pfs_type = pfs_type;
142 pfs->pfs_lockowner = NULL;
143 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
146 case Proot: /* /proc = dr-xr-xr-x */
147 pfs->pfs_mode = (VREAD|VEXEC) |
154 case Pself: /* /proc/self = lr--r--r-- */
155 pfs->pfs_mode = (VREAD) |
162 pfs->pfs_mode = (VREAD|VEXEC) |
169 pfs->pfs_mode = (VREAD|VEXEC) |
176 pfs->pfs_mode = (VREAD|VWRITE) |
191 pfs->pfs_mode = (VREAD) |
198 panic("linprocfs_allocvp");
201 /* add to procfs vnode list */
202 for (pp = &pfshead[pid & PFSHMASK]; *pp; pp = &(*pp)->pfs_next)
206 vx_unlock(vp); /* vnode ready to roll! */
209 pfsvplock &= ~PROCFS_LOCKED;
211 if (pfsvplock & PROCFS_WANT) {
212 pfsvplock &= ~PROCFS_WANT;
213 wakeup((caddr_t) &pfsvplock);
220 linprocfs_freevp(struct vnode *vp)
222 struct pfsnode **pfspp;
223 struct pfsnode *pfs = VTOPFS(vp);
225 pfspp = &pfshead[pfs->pfs_pid & PFSHMASK];
226 while (*pfspp != pfs) {
227 KKASSERT(*pfspp != NULL);
228 pfspp = &(*pfspp)->pfs_next;
230 *pfspp = pfs->pfs_next;
231 FREE(vp->v_data, M_TEMP);
237 linprocfs_rw(struct vop_read_args *ap)
239 struct vnode *vp = ap->a_vp;
240 struct uio *uio = ap->a_uio;
241 struct thread *td = uio->uio_td;
242 struct pfsnode *pfs = VTOPFS(vp);
250 p = PFIND(pfs->pfs_pid);
253 if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE)
256 while (pfs->pfs_lockowner) {
257 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
259 pfs->pfs_lockowner = curthread;
261 switch (pfs->pfs_type) {
263 rtval = procfs_domem(curp, p, pfs, uio);
266 rtval = linprocfs_doprocstat(curp, p, pfs, uio);
269 rtval = linprocfs_doprocstatus(curp, p, pfs, uio);
272 rtval = linprocfs_domeminfo(curp, p, pfs, uio);
275 rtval = linprocfs_docpuinfo(curp, p, pfs, uio);
278 rtval = linprocfs_dostat(curp, p, pfs, uio);
281 rtval = linprocfs_douptime(curp, p, pfs, uio);
284 rtval = linprocfs_doversion(curp, p, pfs, uio);
287 rtval = linprocfs_doloadavg(curp, p, pfs, uio);
293 pfs->pfs_lockowner = NULL;
294 wakeup(&pfs->pfs_lockowner);
300 * Get a string from userland into (buf). Strip a trailing
301 * nl character (to allow easy access from the shell).
302 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
303 * will automatically add a nul char at the end.
305 * Returns 0 on success or the following errors
307 * EINVAL: file offset is non-zero.
308 * EMSGSIZE: message is longer than kernel buffer
309 * EFAULT: user i/o buffer is not addressable
312 vfs_getuserstr(struct uio *uio, char *buf, int *buflenp)
317 if (uio->uio_offset != 0)
322 /* must be able to read the whole string in one go */
323 if (xlen < uio->uio_resid)
325 xlen = uio->uio_resid;
327 if ((error = uiomove(buf, xlen, uio)) != 0)
330 /* allow multiple writes without seeks */
333 /* cleanup string and remove trailing newline */
336 if (xlen > 0 && buf[xlen-1] == '\n')
344 vfs_findname(vfs_namemap_t *nm, char *buf, int buflen)
347 for (; nm->nm_name; nm++)
348 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
356 linprocfs_exit(struct thread *td)
362 KKASSERT(td->td_proc);
363 pid = td->td_proc->p_pid;
366 * Remove all the procfs vnodes associated with an exiting process.
369 for (pfs = pfshead[pid & PFSHMASK]; pfs; pfs = pfs->pfs_next) {
370 if (pfs->pfs_pid == pid) {