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 $
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.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];
57 static struct lwkt_token pfs_token;
60 extern int procfs_domem (struct proc *, struct lwp *, struct pfsnode *pfsp, struct uio *uio);
63 * allocate a pfsnode/vnode pair. the vnode is
64 * referenced, but not locked.
66 * the pid, pfs_type, and mount point uniquely
67 * identify a pfsnode. the mount point is needed
68 * because someone might mount this filesystem
71 * all pfsnodes are maintained on a singly-linked
72 * list. new nodes are only allocated when they cannot
73 * be found on this list. entries on the list are
74 * removed when the vfs reclaim entry is called.
76 * a single lock is kept for the entire list. this is
77 * needed because the getnewvnode() function can block
78 * waiting for a vnode to become free, in which case there
79 * may be more than one process trying to get the same
80 * vnode. this lock is only taken if we are going to
81 * call getnewvnode, since the kernel itself is single-threaded.
83 * if an entry is found on the list, then call vget() to
84 * take a reference. this is done because there may be
85 * zero references to it and so it needs to removed from
86 * the vnode free list.
89 linprocfs_allocvp(struct mount *mp, struct vnode **vpp, long pid,
97 lwkt_gettoken(&pfs_token);
99 for (pfs = pfshead[pid & PFSHMASK]; pfs; pfs = pfs->pfs_next) {
101 if (pfs->pfs_pid == pid &&
102 pfs->pfs_type == pfs_type &&
104 if (vget(vp, LK_EXCLUSIVE|LK_SLEEPFAIL))
107 lwkt_reltoken(&pfs_token);
113 * otherwise lock the vp list while we call getnewvnode
114 * since that can block.
116 if (pfsvplock & PROCFS_LOCKED) {
117 pfsvplock |= PROCFS_WANT;
118 (void) tsleep((caddr_t) &pfsvplock, 0, "pfsavp", 0);
121 pfsvplock |= PROCFS_LOCKED;
124 * Do the MALLOC before the getnewvnode since doing so afterward
125 * might cause a bogus v_data pointer to get dereferenced
126 * elsewhere if MALLOC should block.
128 pfs = kmalloc(sizeof(struct pfsnode), M_TEMP, M_WAITOK);
130 error = getnewvnode(VT_PROCFS, mp, vpp, 0, 0);
140 pfs->pfs_pid = (pid_t) pid;
141 pfs->pfs_type = pfs_type;
144 pfs->pfs_lockowner = NULL;
145 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
148 case Proot: /* /proc = dr-xr-xr-x */
149 vsetflags(vp, VROOT);
154 pfs->pfs_mode = (VREAD|VEXEC) |
160 case Pself: /* /proc/self = lr--r--r-- */
161 pfs->pfs_mode = (VREAD) |
168 pfs->pfs_mode = (VREAD|VEXEC) |
178 pfs->pfs_mode = (VREAD|VEXEC) |
185 pfs->pfs_mode = (VREAD|VWRITE) |
209 pfs->pfs_mode = (VREAD) |
216 panic("linprocfs_allocvp");
219 /* add to procfs vnode list */
220 for (pp = &pfshead[pid & PFSHMASK]; *pp; pp = &(*pp)->pfs_next)
225 pfsvplock &= ~PROCFS_LOCKED;
227 if (pfsvplock & PROCFS_WANT) {
228 pfsvplock &= ~PROCFS_WANT;
229 wakeup((caddr_t) &pfsvplock);
231 lwkt_reltoken(&pfs_token);
237 linprocfs_freevp(struct vnode *vp)
239 struct pfsnode **pfspp;
240 struct pfsnode *pfs = VTOPFS(vp);
242 lwkt_gettoken(&pfs_token);
243 pfspp = &pfshead[pfs->pfs_pid & PFSHMASK];
244 while (*pfspp != pfs) {
245 KKASSERT(*pfspp != NULL);
246 pfspp = &(*pfspp)->pfs_next;
248 *pfspp = pfs->pfs_next;
249 lwkt_reltoken(&pfs_token);
250 kfree(vp->v_data, M_TEMP);
256 * Try to find the calling pid. Note that pfind()
257 * now references the proc structure to be returned
258 * and needs to be released later with PRELE().
261 linprocfs_pfind(pid_t pfs_pid)
263 struct proc *p = NULL;
276 linprocfs_rw(struct vop_read_args *ap)
278 struct vnode *vp = ap->a_vp;
279 struct uio *uio = ap->a_uio;
280 struct thread *td = uio->uio_td;
281 struct pfsnode *pfs = VTOPFS(vp);
290 p = linprocfs_pfind(pfs->pfs_pid);
295 if (p->p_pid == 1 && securelevel > 0 && uio->uio_rw == UIO_WRITE) {
299 lp = FIRST_LWP_IN_PROC(p);
302 lwkt_gettoken(&pfs_token);
303 while (pfs->pfs_lockowner) {
304 tsleep(&pfs->pfs_lockowner, 0, "pfslck", 0);
306 pfs->pfs_lockowner = curthread;
307 lwkt_reltoken(&pfs_token);
309 switch (pfs->pfs_type) {
311 rtval = procfs_domem(curp, lp, pfs, uio);
314 rtval = linprocfs_doprocstat(curp, p, pfs, uio);
317 rtval = linprocfs_doprocstatus(curp, p, pfs, uio);
320 rtval = linprocfs_domeminfo(curp, p, pfs, uio);
323 rtval = linprocfs_docpuinfo(curp, p, pfs, uio);
326 rtval = linprocfs_domounts(curp, p, pfs, uio);
329 rtval = linprocfs_dostat(curp, p, pfs, uio);
332 rtval = linprocfs_douptime(curp, p, pfs, uio);
335 rtval = linprocfs_doversion(curp, p, pfs, uio);
338 rtval = linprocfs_doloadavg(curp, p, pfs, uio);
341 rtval = linprocfs_donetdev(curp, p, pfs, uio);
344 rtval = linprocfs_dodevices(curp, p, pfs, uio);
347 rtval = linprocfs_doosrelease(curp, p, pfs, uio);
350 rtval = linprocfs_doostype(curp, p, pfs, uio);
353 rtval = linprocfs_dopidmax(curp, p, pfs, uio);
356 rtval = linprocfs_domaps(curp, p, pfs, uio);
359 rtval = linprocfs_dostatm(curp, p, pfs, uio);
367 lwkt_gettoken(&pfs_token);
368 pfs->pfs_lockowner = NULL;
369 wakeup(&pfs->pfs_lockowner);
370 lwkt_reltoken(&pfs_token);
380 * Get a string from userland into (buf). Strip a trailing
381 * nl character (to allow easy access from the shell).
382 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
383 * will automatically add a nul char at the end.
385 * Returns 0 on success or the following errors
387 * EINVAL: file offset is non-zero.
388 * EMSGSIZE: message is longer than kernel buffer
389 * EFAULT: user i/o buffer is not addressable
392 vfs_getuserstr(struct uio *uio, char *buf, int *buflenp)
397 if (uio->uio_offset != 0)
402 /* must be able to read the whole string in one go */
403 if (xlen < uio->uio_resid)
405 xlen = uio->uio_resid;
407 if ((error = uiomove(buf, xlen, uio)) != 0)
410 /* allow multiple writes without seeks */
413 /* cleanup string and remove trailing newline */
416 if (xlen > 0 && buf[xlen-1] == '\n')
424 vfs_findname(vfs_namemap_t *nm, char *buf, int buflen)
427 for (; nm->nm_name; nm++)
428 if (bcmp(buf, nm->nm_name, buflen+1) == 0)
436 linprocfs_init(void *arg __unused)
438 lwkt_token_init(&pfs_token, "linprocfs");
440 SYSINIT(linprocfs_init, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
441 linprocfs_init, NULL);
444 linprocfs_exit(struct thread *td)
450 KKASSERT(td->td_proc);
451 pid = td->td_proc->p_pid;
454 * Remove all the procfs vnodes associated with an exiting process.
456 lwkt_gettoken(&pfs_token);
458 for (pfs = pfshead[pid & PFSHMASK]; pfs; pfs = pfs->pfs_next) {
459 if (pfs->pfs_pid == pid) {
462 pfs->pfs_pid |= PFS_DEAD;
467 lwkt_reltoken(&pfs_token);
468 lwkt_token_uninit(&pfs_token);