procfs_subr.c revision 1.14 1 1.14 christos /* $NetBSD: procfs_subr.c,v 1.14 1996/02/09 22:40:51 christos Exp $ */
2 1.13 cgd
3 1.1 pk /*
4 1.5 cgd * Copyright (c) 1993 Jan-Simon Pendry
5 1.11 mycroft * Copyright (c) 1993
6 1.11 mycroft * The Regents of the University of California. All rights reserved.
7 1.2 pk *
8 1.5 cgd * This code is derived from software contributed to Berkeley by
9 1.5 cgd * Jan-Simon Pendry.
10 1.5 cgd *
11 1.2 pk * Redistribution and use in source and binary forms, with or without
12 1.2 pk * modification, are permitted provided that the following conditions
13 1.2 pk * are met:
14 1.2 pk * 1. Redistributions of source code must retain the above copyright
15 1.2 pk * notice, this list of conditions and the following disclaimer.
16 1.2 pk * 2. Redistributions in binary form must reproduce the above copyright
17 1.2 pk * notice, this list of conditions and the following disclaimer in the
18 1.2 pk * documentation and/or other materials provided with the distribution.
19 1.2 pk * 3. All advertising materials mentioning features or use of this software
20 1.2 pk * must display the following acknowledgement:
21 1.5 cgd * This product includes software developed by the University of
22 1.5 cgd * California, Berkeley and its contributors.
23 1.5 cgd * 4. Neither the name of the University nor the names of its contributors
24 1.5 cgd * may be used to endorse or promote products derived from this software
25 1.5 cgd * without specific prior written permission.
26 1.5 cgd *
27 1.5 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.5 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.5 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.5 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.5 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.5 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.5 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.5 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.5 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.5 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.5 cgd * SUCH DAMAGE.
38 1.2 pk *
39 1.13 cgd * @(#)procfs_subr.c 8.5 (Berkeley) 6/15/94
40 1.1 pk */
41 1.5 cgd
42 1.4 mycroft #include <sys/param.h>
43 1.4 mycroft #include <sys/systm.h>
44 1.4 mycroft #include <sys/time.h>
45 1.4 mycroft #include <sys/kernel.h>
46 1.4 mycroft #include <sys/proc.h>
47 1.4 mycroft #include <sys/vnode.h>
48 1.11 mycroft #include <sys/malloc.h>
49 1.5 cgd #include <miscfs/procfs/procfs.h>
50 1.1 pk
51 1.5 cgd static struct pfsnode *pfshead;
52 1.5 cgd static int pfsvplock;
53 1.1 pk
54 1.14 christos int procfs_rw __P((struct vop_read_args *));
55 1.14 christos
56 1.1 pk /*
57 1.5 cgd * allocate a pfsnode/vnode pair. the vnode is
58 1.5 cgd * referenced, but not locked.
59 1.5 cgd *
60 1.5 cgd * the pid, pfs_type, and mount point uniquely
61 1.5 cgd * identify a pfsnode. the mount point is needed
62 1.5 cgd * because someone might mount this filesystem
63 1.5 cgd * twice.
64 1.5 cgd *
65 1.5 cgd * all pfsnodes are maintained on a singly-linked
66 1.5 cgd * list. new nodes are only allocated when they cannot
67 1.5 cgd * be found on this list. entries on the list are
68 1.5 cgd * removed when the vfs reclaim entry is called.
69 1.5 cgd *
70 1.5 cgd * a single lock is kept for the entire list. this is
71 1.5 cgd * needed because the getnewvnode() function can block
72 1.5 cgd * waiting for a vnode to become free, in which case there
73 1.5 cgd * may be more than one process trying to get the same
74 1.5 cgd * vnode. this lock is only taken if we are going to
75 1.5 cgd * call getnewvnode, since the kernel itself is single-threaded.
76 1.5 cgd *
77 1.5 cgd * if an entry is found on the list, then call vget() to
78 1.5 cgd * take a reference. this is done because there may be
79 1.5 cgd * zero references to it and so it needs to removed from
80 1.5 cgd * the vnode free list.
81 1.1 pk */
82 1.11 mycroft int
83 1.5 cgd procfs_allocvp(mp, vpp, pid, pfs_type)
84 1.5 cgd struct mount *mp;
85 1.5 cgd struct vnode **vpp;
86 1.5 cgd long pid;
87 1.5 cgd pfstype pfs_type;
88 1.1 pk {
89 1.12 mycroft struct pfsnode *pfs;
90 1.12 mycroft struct vnode *vp;
91 1.12 mycroft struct pfsnode **pp;
92 1.5 cgd int error;
93 1.5 cgd
94 1.5 cgd loop:
95 1.5 cgd for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) {
96 1.11 mycroft vp = PFSTOV(pfs);
97 1.5 cgd if (pfs->pfs_pid == pid &&
98 1.5 cgd pfs->pfs_type == pfs_type &&
99 1.11 mycroft vp->v_mount == mp) {
100 1.11 mycroft if (vget(vp, 0))
101 1.5 cgd goto loop;
102 1.11 mycroft *vpp = vp;
103 1.5 cgd return (0);
104 1.1 pk }
105 1.1 pk }
106 1.1 pk
107 1.5 cgd /*
108 1.5 cgd * otherwise lock the vp list while we call getnewvnode
109 1.5 cgd * since that can block.
110 1.5 cgd */
111 1.5 cgd if (pfsvplock & PROCFS_LOCKED) {
112 1.5 cgd pfsvplock |= PROCFS_WANT;
113 1.5 cgd sleep((caddr_t) &pfsvplock, PINOD);
114 1.5 cgd goto loop;
115 1.5 cgd }
116 1.5 cgd pfsvplock |= PROCFS_LOCKED;
117 1.5 cgd
118 1.14 christos if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp)) != 0)
119 1.5 cgd goto out;
120 1.11 mycroft vp = *vpp;
121 1.5 cgd
122 1.11 mycroft MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
123 1.11 mycroft vp->v_data = pfs;
124 1.5 cgd
125 1.5 cgd pfs->pfs_next = 0;
126 1.5 cgd pfs->pfs_pid = (pid_t) pid;
127 1.5 cgd pfs->pfs_type = pfs_type;
128 1.11 mycroft pfs->pfs_vnode = vp;
129 1.5 cgd pfs->pfs_flags = 0;
130 1.5 cgd pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
131 1.5 cgd
132 1.5 cgd switch (pfs_type) {
133 1.11 mycroft case Proot: /* /proc = dr-xr-xr-x */
134 1.11 mycroft pfs->pfs_mode = (VREAD|VEXEC) |
135 1.11 mycroft (VREAD|VEXEC) >> 3 |
136 1.11 mycroft (VREAD|VEXEC) >> 6;
137 1.11 mycroft vp->v_type = VDIR;
138 1.11 mycroft vp->v_flag = VROOT;
139 1.11 mycroft break;
140 1.11 mycroft
141 1.11 mycroft case Pcurproc: /* /proc/curproc = lr--r--r-- */
142 1.11 mycroft pfs->pfs_mode = (VREAD) |
143 1.11 mycroft (VREAD >> 3) |
144 1.11 mycroft (VREAD >> 6);
145 1.11 mycroft vp->v_type = VLNK;
146 1.5 cgd break;
147 1.5 cgd
148 1.5 cgd case Pproc:
149 1.5 cgd pfs->pfs_mode = (VREAD|VEXEC) |
150 1.5 cgd (VREAD|VEXEC) >> 3 |
151 1.5 cgd (VREAD|VEXEC) >> 6;
152 1.6 ws vp->v_type = VDIR;
153 1.5 cgd break;
154 1.5 cgd
155 1.11 mycroft case Pfile:
156 1.5 cgd case Pmem:
157 1.5 cgd case Pregs:
158 1.9 cgd case Pfpregs:
159 1.9 cgd pfs->pfs_mode = (VREAD|VWRITE);
160 1.9 cgd vp->v_type = VREG;
161 1.9 cgd break;
162 1.9 cgd
163 1.5 cgd case Pctl:
164 1.11 mycroft case Pnote:
165 1.11 mycroft case Pnotepg:
166 1.11 mycroft pfs->pfs_mode = (VWRITE);
167 1.6 ws vp->v_type = VREG;
168 1.5 cgd break;
169 1.5 cgd
170 1.5 cgd case Pstatus:
171 1.5 cgd pfs->pfs_mode = (VREAD) |
172 1.5 cgd (VREAD >> 3) |
173 1.5 cgd (VREAD >> 6);
174 1.6 ws vp->v_type = VREG;
175 1.5 cgd break;
176 1.5 cgd
177 1.5 cgd default:
178 1.11 mycroft panic("procfs_allocvp");
179 1.5 cgd }
180 1.5 cgd
181 1.5 cgd /* add to procfs vnode list */
182 1.5 cgd for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
183 1.5 cgd continue;
184 1.5 cgd *pp = pfs;
185 1.5 cgd
186 1.5 cgd out:
187 1.5 cgd pfsvplock &= ~PROCFS_LOCKED;
188 1.1 pk
189 1.5 cgd if (pfsvplock & PROCFS_WANT) {
190 1.5 cgd pfsvplock &= ~PROCFS_WANT;
191 1.5 cgd wakeup((caddr_t) &pfsvplock);
192 1.1 pk }
193 1.1 pk
194 1.5 cgd return (error);
195 1.1 pk }
196 1.1 pk
197 1.11 mycroft int
198 1.5 cgd procfs_freevp(vp)
199 1.5 cgd struct vnode *vp;
200 1.1 pk {
201 1.5 cgd struct pfsnode **pfspp;
202 1.5 cgd struct pfsnode *pfs = VTOPFS(vp);
203 1.5 cgd
204 1.5 cgd for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
205 1.5 cgd if (*pfspp == pfs) {
206 1.5 cgd *pfspp = pfs->pfs_next;
207 1.5 cgd break;
208 1.5 cgd }
209 1.1 pk }
210 1.1 pk
211 1.11 mycroft FREE(vp->v_data, M_TEMP);
212 1.11 mycroft vp->v_data = 0;
213 1.5 cgd return (0);
214 1.1 pk }
215 1.1 pk
216 1.11 mycroft int
217 1.11 mycroft procfs_rw(ap)
218 1.11 mycroft struct vop_read_args *ap;
219 1.1 pk {
220 1.11 mycroft struct vnode *vp = ap->a_vp;
221 1.11 mycroft struct uio *uio = ap->a_uio;
222 1.5 cgd struct proc *curp = uio->uio_procp;
223 1.5 cgd struct pfsnode *pfs = VTOPFS(vp);
224 1.5 cgd struct proc *p;
225 1.5 cgd
226 1.5 cgd p = PFIND(pfs->pfs_pid);
227 1.5 cgd if (p == 0)
228 1.1 pk return (EINVAL);
229 1.1 pk
230 1.5 cgd switch (pfs->pfs_type) {
231 1.5 cgd case Pnote:
232 1.5 cgd case Pnotepg:
233 1.11 mycroft return (procfs_donote(curp, p, pfs, uio));
234 1.5 cgd
235 1.5 cgd case Pregs:
236 1.11 mycroft return (procfs_doregs(curp, p, pfs, uio));
237 1.9 cgd
238 1.9 cgd case Pfpregs:
239 1.11 mycroft return (procfs_dofpregs(curp, p, pfs, uio));
240 1.5 cgd
241 1.5 cgd case Pctl:
242 1.11 mycroft return (procfs_doctl(curp, p, pfs, uio));
243 1.5 cgd
244 1.5 cgd case Pstatus:
245 1.11 mycroft return (procfs_dostatus(curp, p, pfs, uio));
246 1.1 pk
247 1.5 cgd case Pmem:
248 1.11 mycroft return (procfs_domem(curp, p, pfs, uio));
249 1.1 pk
250 1.5 cgd default:
251 1.5 cgd return (EOPNOTSUPP);
252 1.5 cgd }
253 1.1 pk }
254 1.1 pk
255 1.5 cgd /*
256 1.5 cgd * Get a string from userland into (buf). Strip a trailing
257 1.5 cgd * nl character (to allow easy access from the shell).
258 1.11 mycroft * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
259 1.5 cgd * will automatically add a nul char at the end.
260 1.5 cgd *
261 1.5 cgd * Returns 0 on success or the following errors
262 1.5 cgd *
263 1.5 cgd * EINVAL: file offset is non-zero.
264 1.5 cgd * EMSGSIZE: message is longer than kernel buffer
265 1.5 cgd * EFAULT: user i/o buffer is not addressable
266 1.5 cgd */
267 1.11 mycroft int
268 1.11 mycroft vfs_getuserstr(uio, buf, buflenp)
269 1.5 cgd struct uio *uio;
270 1.5 cgd char *buf;
271 1.5 cgd int *buflenp;
272 1.1 pk {
273 1.5 cgd int xlen;
274 1.5 cgd int error;
275 1.5 cgd
276 1.11 mycroft if (uio->uio_offset != 0)
277 1.11 mycroft return (EINVAL);
278 1.11 mycroft
279 1.5 cgd xlen = *buflenp;
280 1.1 pk
281 1.5 cgd /* must be able to read the whole string in one go */
282 1.5 cgd if (xlen < uio->uio_resid)
283 1.5 cgd return (EMSGSIZE);
284 1.5 cgd xlen = uio->uio_resid;
285 1.5 cgd
286 1.14 christos if ((error = uiomove(buf, xlen, uio)) != 0)
287 1.5 cgd return (error);
288 1.5 cgd
289 1.11 mycroft /* allow multiple writes without seeks */
290 1.11 mycroft uio->uio_offset = 0;
291 1.11 mycroft
292 1.5 cgd /* cleanup string and remove trailing newline */
293 1.5 cgd buf[xlen] = '\0';
294 1.5 cgd xlen = strlen(buf);
295 1.5 cgd if (xlen > 0 && buf[xlen-1] == '\n')
296 1.5 cgd buf[--xlen] = '\0';
297 1.5 cgd *buflenp = xlen;
298 1.1 pk
299 1.5 cgd return (0);
300 1.1 pk }
301 1.1 pk
302 1.11 mycroft vfs_namemap_t *
303 1.11 mycroft vfs_findname(nm, buf, buflen)
304 1.11 mycroft vfs_namemap_t *nm;
305 1.5 cgd char *buf;
306 1.5 cgd int buflen;
307 1.1 pk {
308 1.11 mycroft
309 1.5 cgd for (; nm->nm_name; nm++)
310 1.5 cgd if (bcmp(buf, (char *) nm->nm_name, buflen+1) == 0)
311 1.5 cgd return (nm);
312 1.5 cgd
313 1.5 cgd return (0);
314 1.1 pk }
315