procfs_subr.c revision 1.5 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.5 1994/01/05 07:51:29 cgd 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 = *vpp;
126 pfs->pfs_flags = 0;
127 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
128
129 switch (pfs_type) {
130 case Proot: /* /proc = dr-xr-xr-x */
131 pfs->pfs_mode = (VREAD|VEXEC) |
132 (VREAD|VEXEC) >> 3 |
133 (VREAD|VEXEC) >> 6;
134 break;
135
136 case Pproc:
137 pfs->pfs_mode = (VREAD|VEXEC) |
138 (VREAD|VEXEC) >> 3 |
139 (VREAD|VEXEC) >> 6;
140 break;
141
142 case Pfile:
143 pfs->pfs_mode = (VREAD|VWRITE);
144 break;
145
146 case Pmem:
147 pfs->pfs_mode = (VREAD|VWRITE);
148 break;
149
150 case Pregs:
151 pfs->pfs_mode = (VREAD|VWRITE);
152 break;
153
154 case Pctl:
155 pfs->pfs_mode = (VWRITE);
156 break;
157
158 case Pstatus:
159 pfs->pfs_mode = (VREAD) |
160 (VREAD >> 3) |
161 (VREAD >> 6);
162 break;
163
164 case Pnote:
165 pfs->pfs_mode = (VWRITE);
166 break;
167
168 case Pnotepg:
169 pfs->pfs_mode = (VWRITE);
170 break;
171
172 default:
173 panic("procfs_allocvp");
174 }
175
176 /* add to procfs vnode list */
177 for (pp = &pfshead; *pp; pp = &(*pp)->pfs_next)
178 continue;
179 *pp = pfs;
180
181 out:
182 pfsvplock &= ~PROCFS_LOCKED;
183
184 if (pfsvplock & PROCFS_WANT) {
185 pfsvplock &= ~PROCFS_WANT;
186 wakeup((caddr_t) &pfsvplock);
187 }
188
189 return (error);
190 }
191
192 procfs_freevp(vp)
193 struct vnode *vp;
194 {
195 struct pfsnode **pfspp;
196 struct pfsnode *pfs = VTOPFS(vp);
197
198 /* 4.4: at this point, need to deallocate the pfsnode */
199
200 for (pfspp = &pfshead; *pfspp != 0; pfspp = &(*pfspp)->pfs_next) {
201 if (*pfspp == pfs) {
202 *pfspp = pfs->pfs_next;
203 break;
204 }
205 }
206
207 return (0);
208 }
209
210 procfs_rw(vp, uio, ioflag, cred)
211 struct vnode *vp;
212 struct uio *uio;
213 int ioflag;
214 struct ucred *cred;
215 {
216 struct proc *curp = uio->uio_procp;
217 struct pfsnode *pfs = VTOPFS(vp);
218 struct proc *p;
219
220 p = PFIND(pfs->pfs_pid);
221 if (p == 0)
222 return (EINVAL);
223
224 switch (pfs->pfs_type) {
225 case Pnote:
226 case Pnotepg:
227 return (pfs_donote(curp, p, pfs, uio));
228
229 case Pregs:
230 return (pfs_doregs(curp, p, pfs, uio));
231
232 case Pctl:
233 return (pfs_doctl(curp, p, pfs, uio));
234
235 case Pstatus:
236 return (pfs_dostatus(curp, p, pfs, uio));
237
238 case Pmem:
239 return (pfs_domem(curp, p, pfs, uio));
240
241 default:
242 return (EOPNOTSUPP);
243 }
244 }
245
246 /*
247 * Get a string from userland into (buf). Strip a trailing
248 * nl character (to allow easy access from the shell).
249 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
250 * will automatically add a nul char at the end.
251 *
252 * Returns 0 on success or the following errors
253 *
254 * EINVAL: file offset is non-zero.
255 * EMSGSIZE: message is longer than kernel buffer
256 * EFAULT: user i/o buffer is not addressable
257 */
258 vfs_getuserstr(uio, buf, buflenp)
259 struct uio *uio;
260 char *buf;
261 int *buflenp;
262 {
263 int xlen;
264 int error;
265
266 if (uio->uio_offset != 0)
267 return (EINVAL);
268
269 xlen = *buflenp;
270
271 /* must be able to read the whole string in one go */
272 if (xlen < uio->uio_resid)
273 return (EMSGSIZE);
274 xlen = uio->uio_resid;
275
276 error = uiomove(buf, xlen, uio);
277 if (error)
278 return (error);
279
280 /* allow multiple writes without seeks */
281 uio->uio_offset = 0;
282
283 /* cleanup string and remove trailing newline */
284 buf[xlen] = '\0';
285 xlen = strlen(buf);
286 if (xlen > 0 && buf[xlen-1] == '\n')
287 buf[--xlen] = '\0';
288 *buflenp = xlen;
289
290 return (0);
291 }
292
293 vfs_namemap_t *
294 vfs_findname(nm, buf, buflen)
295 vfs_namemap_t *nm;
296 char *buf;
297 int buflen;
298 {
299 for (; nm->nm_name; nm++)
300 if (bcmp(buf, (char *) nm->nm_name, buflen+1) == 0)
301 return (nm);
302
303 return (0);
304 }
305