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