procfs_subr.c revision 1.6 1 /*
2 * Copyright (c) 1993 The Regents of the University of California.
3 * Copyright (c) 1993 Jan-Simon Pendry
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Jan-Simon Pendry.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
24 *
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
35 * SUCH DAMAGE.
36 *
37 * From:
38 * Id: procfs_subr.c,v 4.1 1993/12/17 10:47:45 jsp Rel
39 *
40 * $Id: procfs_subr.c,v 1.6 1994/01/09 19:44:07 ws Exp $
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/time.h>
46 #include <sys/kernel.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <miscfs/procfs/procfs.h>
50
51 static struct pfsnode *pfshead;
52 static int pfsvplock;
53
54 /*
55 * allocate a pfsnode/vnode pair. the vnode is
56 * referenced, but not locked.
57 *
58 * the pid, pfs_type, and mount point uniquely
59 * identify a pfsnode. the mount point is needed
60 * because someone might mount this filesystem
61 * twice.
62 *
63 * all pfsnodes are maintained on a singly-linked
64 * list. new nodes are only allocated when they cannot
65 * be found on this list. entries on the list are
66 * removed when the vfs reclaim entry is called.
67 *
68 * a single lock is kept for the entire list. this is
69 * needed because the getnewvnode() function can block
70 * waiting for a vnode to become free, in which case there
71 * may be more than one process trying to get the same
72 * vnode. this lock is only taken if we are going to
73 * call getnewvnode, since the kernel itself is single-threaded.
74 *
75 * if an entry is found on the list, then call vget() to
76 * take a reference. this is done because there may be
77 * zero references to it and so it needs to removed from
78 * the vnode free list.
79 */
80 procfs_allocvp(mp, vpp, pid, pfs_type)
81 struct mount *mp;
82 struct vnode **vpp;
83 long pid;
84 pfstype pfs_type;
85 {
86 int error;
87 struct pfsnode *pfs;
88 struct pfsnode **pp;
89 struct vnode *vp;
90
91 loop:
92 for (pfs = pfshead; pfs != 0; pfs = pfs->pfs_next) {
93 if (pfs->pfs_pid == pid &&
94 pfs->pfs_type == pfs_type &&
95 PFSTOV(pfs)->v_mount == mp) {
96 if (vget(pfs->pfs_vnode))
97 goto loop;
98 VOP_UNLOCK(pfs->pfs_vnode);
99 *vpp = pfs->pfs_vnode;
100 return (0);
101 }
102 }
103
104 /*
105 * otherwise lock the vp list while we call getnewvnode
106 * since that can block.
107 */
108 if (pfsvplock & PROCFS_LOCKED) {
109 pfsvplock |= PROCFS_WANT;
110 sleep((caddr_t) &pfsvplock, PINOD);
111 goto loop;
112 }
113 pfsvplock |= PROCFS_LOCKED;
114
115 error = getnewvnode(VT_PROCFS, mp, &procfs_vnodeops, vpp);
116 if (error)
117 goto out;
118
119 /* 4.4: at this point, need to allocate a pfsnode */
120
121 pfs = VTOPFS(*vpp);
122 pfs->pfs_next = 0;
123 pfs->pfs_pid = (pid_t) pid;
124 pfs->pfs_type = pfs_type;
125 pfs->pfs_vnode = vp = *vpp;
126 pfs->pfs_flags = 0;
127 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
128
129 switch (pfs_type) {
130 case Proot:
131 switch ((int)pid) {
132 case 0: /* /proc = dr-xr-xr-x */
133 pfs->pfs_mode = (VREAD|VEXEC) |
134 (VREAD|VEXEC) >> 3 |
135 (VREAD|VEXEC) >> 6;
136 vp->v_type = VDIR;
137 vp->v_flag = VROOT;
138 break;
139 case 1: /* /proc/curdir = lr--r--r-- */
140 pfs->pfs_mode = VREAD |
141 VREAD >> 3 |
142 VREAD >> 6;
143 vp->v_type = VLNK;
144 default:
145 panic("procfs_allocvp root");
146 }
147 break;
148
149 case Pproc:
150 pfs->pfs_mode = (VREAD|VEXEC) |
151 (VREAD|VEXEC) >> 3 |
152 (VREAD|VEXEC) >> 6;
153 vp->v_type = VDIR;
154 break;
155
156 case Pmem:
157 pfs->pfs_mode = (VREAD|VWRITE);
158 vp->v_type = VREG;
159 break;
160
161 case Pregs:
162 pfs->pfs_mode = (VREAD|VWRITE);
163 vp->v_type = VREG;
164 break;
165
166 case Pctl:
167 pfs->pfs_mode = (VWRITE);
168 vp->v_type = VREG;
169 break;
170
171 case Pstatus:
172 pfs->pfs_mode = (VREAD) |
173 (VREAD >> 3) |
174 (VREAD >> 6);
175 vp->v_type = VREG;
176 break;
177
178 case Pnote:
179 pfs->pfs_mode = (VWRITE);
180 vp->v_type = VREG;
181 break;
182
183 case Pnotepg:
184 pfs->pfs_mode = (VWRITE);
185 vp->v_type = VREG;
186 break;
187
188 default:
189 panic("procfs_allocvp type");
190 }
191
192 /* add to procfs vnode list */
193 for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
194 continue;
195 *pp = pfs;
196
197 out:
198 pfsvplock &= ~PROCFS_LOCKED;
199
200 if (pfsvplock & PROCFS_WANT) {
201 pfsvplock &= ~PROCFS_WANT;
202 wakeup((caddr_t) &pfsvplock);
203 }
204
205 return (error);
206 }
207
208 procfs_freevp(vp)
209 struct vnode *vp;
210 {
211 struct pfsnode **pfspp;
212 struct pfsnode *pfs = VTOPFS(vp);
213
214 /* 4.4: at this point, need to deallocate the pfsnode */
215
216 for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
217 if (*pfspp == pfs) {
218 *pfspp = pfs->pfs_next;
219 break;
220 }
221 }
222
223 return (0);
224 }
225
226 procfs_rw(vp, uio, ioflag, cred)
227 struct vnode *vp;
228 struct uio *uio;
229 int ioflag;
230 struct ucred *cred;
231 {
232 struct proc *curp = uio->uio_procp;
233 struct pfsnode *pfs = VTOPFS(vp);
234 struct proc *p;
235
236 p = PFIND(pfs->pfs_pid);
237 if (p == 0)
238 return (EINVAL);
239
240 switch (pfs->pfs_type) {
241 case Pnote:
242 case Pnotepg:
243 return (pfs_donote(curp, p, pfs, uio));
244
245 case Pregs:
246 return (pfs_doregs(curp, p, pfs, uio));
247
248 case Pctl:
249 return (pfs_doctl(curp, p, pfs, uio));
250
251 case Pstatus:
252 return (pfs_dostatus(curp, p, pfs, uio));
253
254 case Pmem:
255 return (pfs_domem(curp, p, pfs, uio));
256
257 default:
258 return (EOPNOTSUPP);
259 }
260 }
261
262 /*
263 * Get a string from userland into (buf). Strip a trailing
264 * nl character (to allow easy access from the shell).
265 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
266 * will automatically add a nul char at the end.
267 *
268 * Returns 0 on success or the following errors
269 *
270 * EINVAL: file offset is non-zero.
271 * EMSGSIZE: message is longer than kernel buffer
272 * EFAULT: user i/o buffer is not addressable
273 */
274 vfs_getuserstr(uio, buf, buflenp)
275 struct uio *uio;
276 char *buf;
277 int *buflenp;
278 {
279 int xlen;
280 int error;
281
282 if (uio->uio_offset != 0)
283 return (EINVAL);
284
285 xlen = *buflenp;
286
287 /* must be able to read the whole string in one go */
288 if (xlen < uio->uio_resid)
289 return (EMSGSIZE);
290 xlen = uio->uio_resid;
291
292 error = uiomove(buf, xlen, uio);
293 if (error)
294 return (error);
295
296 /* allow multiple writes without seeks */
297 uio->uio_offset = 0;
298
299 /* cleanup string and remove trailing newline */
300 buf[xlen] = '\0';
301 xlen = strlen(buf);
302 if (xlen > 0 && buf[xlen-1] == '\n')
303 buf[--xlen] = '\0';
304 *buflenp = xlen;
305
306 return (0);
307 }
308
309 vfs_namemap_t *
310 vfs_findname(nm, buf, buflen)
311 vfs_namemap_t *nm;
312 char *buf;
313 int buflen;
314 {
315 for (; nm->nm_name; nm++)
316 if (bcmp(buf, (char *) nm->nm_name, buflen+1) == 0)
317 return (nm);
318
319 return (0);
320 }
321