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.7 2004/03/01 06:33:15 dillon Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/vnode.h>
49 #include <sys/malloc.h>
50 #include "linprocfs.h"
52 static struct pfsnode *pfshead;
55 extern int procfs_domem (struct proc *, struct proc *, struct pfsnode *pfsp, struct uio *uio);
58 * allocate a pfsnode/vnode pair. the vnode is
59 * referenced, but not locked.
61 * the pid, pfs_type, and mount point uniquely
62 * identify a pfsnode. the mount point is needed
63 * because someone might mount this filesystem
66 * all pfsnodes are maintained on a singly-linked
67 * list. new nodes are only allocated when they cannot
68 * be found on this list. entries on the list are
69 * removed when the vfs reclaim entry is called.
71 * a single lock is kept for the entire list. this is
72 * needed because the getnewvnode() function can block
73 * waiting for a vnode to become free, in which case there
74 * may be more than one process trying to get the same
75 * vnode. this lock is only taken if we are going to
76 * call getnewvnode, since the kernel itself is single-threaded.
78 * if an entry is found on the list, then call vget() to
79 * take a reference. this is done because there may be
80 * zero references to it and so it needs to removed from
81 * the vnode free list.
84 linprocfs_allocvp(mp, vpp, pid, pfs_type)
90 struct thread *td = curthread; /* XXX */
97 for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) {
99 if (pfs->pfs_pid == pid &&
100 pfs->pfs_type == pfs_type &&
102 if (vget(vp, NULL, 0, td))
110 * otherwise lock the vp list while we call getnewvnode
111 * since that can block.
113 if (pfsvplock & PROCFS_LOCKED) {
114 pfsvplock |= PROCFS_WANT;
115 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
118 pfsvplock |= PROCFS_LOCKED;
121 * Do the MALLOC before the getnewvnode since doing so afterward
122 * might cause a bogus v_data pointer to get dereferenced
123 * elsewhere if MALLOC should block.
125 MALLOC(pfs, struct pfsnode *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
127 if ((error = getnewvnode(VT_PROCFS, mp, linprocfs_vnodeop_p, vpp)) != 0) {
136 pfs->pfs_pid = (pid_t) pid;
137 pfs->pfs_type = pfs_type;
140 pfs->pfs_lockowner = NULL;
141 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
144 case Proot: /* /proc = dr-xr-xr-x */
145 pfs->pfs_mode = (VREAD|VEXEC) |
152 case Pself: /* /proc/self = lr--r--r-- */
153 pfs->pfs_mode = (VREAD) |
160 pfs->pfs_mode = (VREAD|VEXEC) |
167 pfs->pfs_mode = (VREAD|VEXEC) |
174 pfs->pfs_mode = (VREAD|VWRITE) |
189 pfs->pfs_mode = (VREAD) |
196 panic("linprocfs_allocvp");
199 /* add to procfs vnode list */
200 for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
205 pfsvplock &= ~PROCFS_LOCKED;
207 if (pfsvplock & PROCFS_WANT) {
208 pfsvplock &= ~PROCFS_WANT;
209 wakeup((caddr_t) &pfsvplock);
219 struct pfsnode **pfspp;
220 struct pfsnode *pfs = VTOPFS(vp);
222 for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
224 *pfspp = pfs->pfs_next;
229 FREE(vp->v_data, M_TEMP);
236 struct vop_read_args *ap;
238 struct vnode *vp = ap->a_vp;
239 struct uio *uio = ap->a_uio;
240 struct thread *td = uio->uio_td;
241 struct pfsnode *pfs = VTOPFS(vp);
249 p = PFIND(pfs->pfs_pid);
252 if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE)
255 while (pfs->pfs_lockowner) {
256 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
258 pfs->pfs_lockowner = curthread;
260 switch (pfs->pfs_type) {
262 rtval = procfs_domem(curp, p, pfs, uio);
265 rtval = linprocfs_doprocstat(curp, p, pfs, uio);
268 rtval = linprocfs_doprocstatus(curp, p, pfs, uio);
271 rtval = linprocfs_domeminfo(curp, p, pfs, uio);
274 rtval = linprocfs_docpuinfo(curp, p, pfs, uio);
277 rtval = linprocfs_dostat(curp, p, pfs, uio);
280 rtval = linprocfs_douptime(curp, p, pfs, uio);
283 rtval = linprocfs_doversion(curp, p, pfs, uio);
286 rtval = linprocfs_doloadavg(curp, p, pfs, uio);
292 pfs->pfs_lockowner = NULL;
293 wakeup(&pfs->pfs_lockowner);
299 * Get a string from userland into (buf). Strip a trailing
300 * nl character (to allow easy access from the shell).
301 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
302 * will automatically add a nul char at the end.
304 * Returns 0 on success or the following errors
306 * EINVAL: file offset is non-zero.
307 * EMSGSIZE: message is longer than kernel buffer
308 * EFAULT: user i/o buffer is not addressable
311 vfs_getuserstr(uio, buf, buflenp)
319 if (uio->uio_offset != 0)
324 /* must be able to read the whole string in one go */
325 if (xlen < uio->uio_resid)
327 xlen = uio->uio_resid;
329 if ((error = uiomove(buf, xlen, uio)) != 0)
332 /* allow multiple writes without seeks */
335 /* cleanup string and remove trailing newline */
338 if (xlen > 0 && buf[xlen-1] == '\n')
346 vfs_findname(nm, buf, buflen)
352 for (; nm->nm_name; nm++)
353 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
361 linprocfs_exit(struct thread *td)
364 pid_t pid = (td->td_proc) ? td->td_proc->p_pid : -1; /* YYY */
367 * The reason for this loop is not obvious -- basicly,
368 * linprocfs_freevp(), which is called via vgone() (eventually),
369 * removes the specified procfs node from the pfshead list.
370 * It does this by *pfsp = pfs->pfs_next, meaning that it
371 * overwrites the node. So when we do pfs = pfs->next, we
372 * end up skipping the node that replaces the one that was
373 * vgone'd. Since it may have been the last one on the list,
374 * it may also have been set to null -- but *our* pfs pointer,
375 * here, doesn't see this. So the loop starts from the beginning
378 * This is not a for() loop because the final event
379 * would be "pfs = pfs->pfs_next"; in the case where
380 * pfs is set to pfshead again, that would mean that
381 * pfshead is skipped over.
386 if (pfs->pfs_pid == pid) {