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