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>
51 #include <vfs/procfs/procfs.h>
54 #define PFS_HMASK (PFS_HSIZE - 1)
56 static struct pfsnode
*pfshead
[PFS_HSIZE
];
59 #define PFSHASH(pid) &pfshead[(pid) & PFS_HMASK]
62 * Allocate a pfsnode/vnode pair. If no error occurs the returned vnode
63 * will be referenced and exclusively locked.
65 * The pid, pfs_type, and mount point uniquely identify a pfsnode.
66 * The mount point is needed because someone might mount this filesystem
69 * All pfsnodes are maintained on a singly-linked list. new nodes are
70 * only allocated when they cannot be found on this list. entries on
71 * the list are removed when the vfs reclaim entry is called.
73 * A single lock is kept for the entire list. this is needed because the
74 * getnewvnode() function can block waiting for a vnode to become free,
75 * in which case there may be more than one process trying to get the same
76 * vnode. this lock is only taken if we are going to call getnewvnode,
77 * since the kernel itself is single-threaded.
79 * If an entry is found on the list, then call vget() to take a reference
80 * and obtain the lock. This will properly re-reference the vnode if it
81 * had gotten onto the free list.
84 procfs_allocvp(struct mount
*mp
, struct vnode
**vpp
, long pid
, pfstype pfs_type
)
93 for (pfs
= *pp
; pfs
; pfs
= pfs
->pfs_next
) {
94 if (pfs
->pfs_pid
== pid
&& pfs
->pfs_type
== pfs_type
&&
95 PFSTOV(pfs
)->v_mount
== mp
) {
97 if (vget(vp
, LK_EXCLUSIVE
))
101 * Make sure the vnode is still in the cache after
102 * getting the interlock to avoid racing a free.
104 for (pfs
= *pp
; pfs
; pfs
= pfs
->pfs_next
) {
105 if (PFSTOV(pfs
) == vp
&&
106 pfs
->pfs_pid
== pid
&&
107 pfs
->pfs_type
== pfs_type
&&
108 PFSTOV(pfs
)->v_mount
== mp
) {
112 if (pfs
== NULL
|| PFSTOV(pfs
) != vp
) {
123 * otherwise lock the vp list while we call getnewvnode
124 * since that can block.
126 if (pfsvplock
& PROCFS_LOCKED
) {
127 pfsvplock
|= PROCFS_WANT
;
128 (void) tsleep((caddr_t
) &pfsvplock
, 0, "pfsavp", 0);
131 pfsvplock
|= PROCFS_LOCKED
;
134 * Do the MALLOC before the getnewvnode since doing so afterward
135 * might cause a bogus v_data pointer to get dereferenced
136 * elsewhere if MALLOC should block.
138 * XXX this may not matter anymore since getnewvnode now returns
141 MALLOC(pfs
, struct pfsnode
*, sizeof(struct pfsnode
), M_TEMP
, M_WAITOK
);
143 error
= getnewvnode(VT_PROCFS
, mp
, vpp
, 0, 0);
153 pfs
->pfs_pid
= (pid_t
) pid
;
154 pfs
->pfs_type
= pfs_type
;
157 pfs
->pfs_lockowner
= 0;
158 pfs
->pfs_fileno
= PROCFS_FILENO(pid
, pfs_type
);
161 case Proot
: /* /proc = dr-xr-xr-x */
162 pfs
->pfs_mode
= (VREAD
|VEXEC
) |
169 case Pcurproc
: /* /proc/curproc = lr--r--r-- */
170 pfs
->pfs_mode
= (VREAD
) |
177 pfs
->pfs_mode
= (VREAD
|VEXEC
) |
184 pfs
->pfs_mode
= (VREAD
|VEXEC
) |
191 pfs
->pfs_mode
= (VREAD
|VWRITE
);
198 pfs
->pfs_mode
= (VREAD
|VWRITE
);
205 pfs
->pfs_mode
= (VWRITE
);
214 pfs
->pfs_mode
= (VREAD
) |
221 panic("procfs_allocvp");
224 /* add to procfs vnode list */
229 pfsvplock
&= ~PROCFS_LOCKED
;
231 if (pfsvplock
& PROCFS_WANT
) {
232 pfsvplock
&= ~PROCFS_WANT
;
233 wakeup((caddr_t
) &pfsvplock
);
240 procfs_freevp(struct vnode
*vp
)
242 struct pfsnode
**pfspp
;
248 pfspp
= PFSHASH(pfs
->pfs_pid
);
249 while (*pfspp
!= pfs
&& *pfspp
)
250 pfspp
= &(*pfspp
)->pfs_next
;
252 *pfspp
= pfs
->pfs_next
;
253 pfs
->pfs_next
= NULL
;
259 procfs_rw(struct vop_read_args
*ap
)
261 struct vnode
*vp
= ap
->a_vp
;
262 struct uio
*uio
= ap
->a_uio
;
263 struct thread
*curtd
= uio
->uio_td
;
265 struct pfsnode
*pfs
= VTOPFS(vp
);
272 if ((curp
= curtd
->td_proc
) == NULL
) /* XXX */
275 p
= PFIND(pfs
->pfs_pid
);
278 if (p
->p_pid
== 1 && securelevel
> 0 && uio
->uio_rw
== UIO_WRITE
)
281 lp
= FIRST_LWP_IN_PROC(p
);
284 while (pfs
->pfs_lockowner
) {
285 tsleep(&pfs
->pfs_lockowner
, 0, "pfslck", 0);
287 pfs
->pfs_lockowner
= curproc
->p_pid
;
289 switch (pfs
->pfs_type
) {
292 rtval
= procfs_donote(curp
, lp
, pfs
, uio
);
296 rtval
= procfs_doregs(curp
, lp
, pfs
, uio
);
300 rtval
= procfs_dofpregs(curp
, lp
, pfs
, uio
);
304 rtval
= procfs_dodbregs(curp
, lp
, pfs
, uio
);
308 rtval
= procfs_doctl(curp
, lp
, pfs
, uio
);
312 rtval
= procfs_dostatus(curp
, lp
, pfs
, uio
);
316 rtval
= procfs_domap(curp
, lp
, pfs
, uio
);
320 rtval
= procfs_domem(curp
, lp
, pfs
, uio
);
324 rtval
= procfs_dotype(curp
, lp
, pfs
, uio
);
328 rtval
= procfs_docmdline(curp
, lp
, pfs
, uio
);
332 rtval
= procfs_dorlimit(curp
, lp
, pfs
, uio
);
340 pfs
->pfs_lockowner
= 0;
341 wakeup(&pfs
->pfs_lockowner
);
346 * Get a string from userland into (buf). Strip a trailing
347 * nl character (to allow easy access from the shell).
348 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
349 * will automatically add a nul char at the end.
351 * Returns 0 on success or the following errors
353 * EINVAL: file offset is non-zero.
354 * EMSGSIZE: message is longer than kernel buffer
355 * EFAULT: user i/o buffer is not addressable
358 vfs_getuserstr(struct uio
*uio
, char *buf
, int *buflenp
)
363 if (uio
->uio_offset
!= 0)
368 /* must be able to read the whole string in one go */
369 if (xlen
< uio
->uio_resid
)
371 xlen
= uio
->uio_resid
;
373 if ((error
= uiomove(buf
, xlen
, uio
)) != 0)
376 /* allow multiple writes without seeks */
379 /* cleanup string and remove trailing newline */
382 if (xlen
> 0 && buf
[xlen
-1] == '\n')
390 vfs_findname(vfs_namemap_t
*nm
, char *buf
, int buflen
)
393 for (; nm
->nm_name
; nm
++)
394 if (bcmp(buf
, nm
->nm_name
, buflen
+1) == 0)
401 procfs_exit(struct thread
*td
)
407 KKASSERT(td
->td_proc
);
408 pid
= td
->td_proc
->p_pid
;
411 * The reason for this loop is not obvious -- basicly,
412 * procfs_freevp(), which is called via vgone() (eventually),
413 * removes the specified procfs node from the pfshead list.
414 * It does this by *pfsp = pfs->pfs_next, meaning that it
415 * overwrites the node. So when we do pfs = pfs->next, we
416 * end up skipping the node that replaces the one that was
417 * vgone'd. Since it may have been the last one on the list,
418 * it may also have been set to null -- but *our* pfs pointer,
419 * here, doesn't see this. So the loop starts from the beginning
422 * This is not a for() loop because the final event
423 * would be "pfs = pfs->pfs_next"; in the case where
424 * pfs is set to pfshead again, that would mean that
425 * pfshead is skipped over.
431 if (pfs
->pfs_pid
== pid
) {