procfs_subr.c revision 1.36.2.2 1 /* $NetBSD: procfs_subr.c,v 1.36.2.2 2001/04/09 01:58:09 nathanw 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 void procfs_hashins __P((struct pfsnode *));
55 void procfs_hashrem __P((struct pfsnode *));
56 struct vnode *procfs_hashget __P((pid_t, pfstype, struct mount *));
57
58 LIST_HEAD(pfs_hashhead, pfsnode) *pfs_hashtbl;
59 u_long pfs_ihash; /* size of hash table - 1 */
60 #define PFSPIDHASH(pid) (&pfs_hashtbl[(pid) & pfs_ihash])
61
62 struct lock pfs_hashlock;
63 struct simplelock pfs_hash_slock;
64
65 #define ISSET(t, f) ((t) & (f))
66
67 /*
68 * allocate a pfsnode/vnode pair. the vnode is
69 * referenced, and locked.
70 *
71 * the pid, pfs_type, and mount point uniquely
72 * identify a pfsnode. the mount point is needed
73 * because someone might mount this filesystem
74 * twice.
75 *
76 * all pfsnodes are maintained on a singly-linked
77 * list. new nodes are only allocated when they cannot
78 * be found on this list. entries on the list are
79 * removed when the vfs reclaim entry is called.
80 *
81 * a single lock is kept for the entire list. this is
82 * needed because the getnewvnode() function can block
83 * waiting for a vnode to become free, in which case there
84 * may be more than one process trying to get the same
85 * vnode. this lock is only taken if we are going to
86 * call getnewvnode, since the kernel itself is single-threaded.
87 *
88 * if an entry is found on the list, then call vget() to
89 * take a reference. this is done because there may be
90 * zero references to it and so it needs to removed from
91 * the vnode free list.
92 */
93 int
94 procfs_allocvp(mp, vpp, pid, pfs_type)
95 struct mount *mp;
96 struct vnode **vpp;
97 long pid;
98 pfstype pfs_type;
99 {
100 struct pfsnode *pfs;
101 struct vnode *vp;
102 int error;
103
104 do {
105 if ((*vpp = procfs_hashget(pid, pfs_type, mp)) != NULL)
106 return (0);
107 } while (lockmgr(&pfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
108
109 if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, vpp)) != 0) {
110 *vpp = NULL;
111 lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
112 return (error);
113 }
114 vp = *vpp;
115
116 MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
117 vp->v_data = pfs;
118
119 pfs->pfs_pid = (pid_t) pid;
120 pfs->pfs_type = pfs_type;
121 pfs->pfs_vnode = vp;
122 pfs->pfs_flags = 0;
123 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type);
124
125 switch (pfs_type) {
126 case Proot: /* /proc = dr-xr-xr-x */
127 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
128 vp->v_type = VDIR;
129 vp->v_flag = VROOT;
130 break;
131
132 case Pcurproc: /* /proc/curproc = lr-xr-xr-x */
133 case Pself: /* /proc/self = lr-xr-xr-x */
134 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
135 vp->v_type = VLNK;
136 break;
137
138 case Pproc: /* /proc/N = dr-xr-xr-x */
139 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
140 vp->v_type = VDIR;
141 break;
142
143 case Pfile: /* /proc/N/file = -rw------- */
144 case Pmem: /* /proc/N/mem = -rw------- */
145 case Pregs: /* /proc/N/regs = -rw------- */
146 case Pfpregs: /* /proc/N/fpregs = -rw------- */
147 pfs->pfs_mode = S_IRUSR|S_IWUSR;
148 vp->v_type = VREG;
149 break;
150
151 case Pctl: /* /proc/N/ctl = --w------ */
152 case Pnote: /* /proc/N/note = --w------ */
153 case Pnotepg: /* /proc/N/notepg = --w------ */
154 pfs->pfs_mode = S_IWUSR;
155 vp->v_type = VREG;
156 break;
157
158 case Pmap: /* /proc/N/map = -r--r--r-- */
159 case Pmaps: /* /proc/N/maps = -r--r--r-- */
160 case Pstatus: /* /proc/N/status = -r--r--r-- */
161 case Pcmdline: /* /proc/N/cmdline = -r--r--r-- */
162 case Pmeminfo: /* /proc/meminfo = -r--r--r-- */
163 case Pcpuinfo: /* /proc/cpuinfo = -r--r--r-- */
164 pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
165 vp->v_type = VREG;
166 break;
167
168 default:
169 panic("procfs_allocvp");
170 }
171
172 procfs_hashins(pfs);
173 uvm_vnp_setsize(vp, 0);
174 lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
175
176 return (error);
177 }
178
179 int
180 procfs_freevp(vp)
181 struct vnode *vp;
182 {
183 struct pfsnode *pfs = VTOPFS(vp);
184
185 procfs_hashrem(pfs);
186
187 FREE(vp->v_data, M_TEMP);
188 vp->v_data = 0;
189 return (0);
190 }
191
192 int
193 procfs_rw(v)
194 void *v;
195 {
196 struct vop_read_args *ap = v;
197 struct vnode *vp = ap->a_vp;
198 struct uio *uio = ap->a_uio;
199 struct proc *curp = uio->uio_procp;
200 struct pfsnode *pfs = VTOPFS(vp);
201 struct lwp *l;
202 struct proc *p;
203
204 p = PFIND(pfs->pfs_pid);
205 if (p == 0)
206 return (EINVAL);
207
208 /* XXX NJWLWP
209 * The entire procfs interface needs work to be useful to
210 * a process with multiple LWPs. For the moment, we'll
211 * just kluge this and fail on others.
212 */
213
214 if (p->p_nlwps > 1)
215 switch (pfs->pfs_type) {
216 case Pnote:
217 case Pnotepg:
218 case Pstatus:
219 case Pmap:
220 case Pmem:
221 case Pcmdline:
222 break;
223 case Pregs:
224 case Pfpregs:
225 case Pctl:
226 default:
227 return (EOPNOTSUPP);
228 }
229
230 l = LIST_FIRST(&p->p_lwps);
231
232 switch (pfs->pfs_type) {
233 case Pregs:
234 case Pfpregs:
235 case Pmem:
236 /*
237 * Do not allow init to be modified while in secure mode; it
238 * could be duped into changing the security level.
239 */
240 if (uio->uio_rw == UIO_WRITE &&
241 p == initproc && securelevel > -1)
242 return (EPERM);
243 break;
244
245 default:
246 break;
247 }
248
249 switch (pfs->pfs_type) {
250 case Pnote:
251 case Pnotepg:
252 return (procfs_donote(curp, p, pfs, uio));
253
254 case Pregs:
255 return (procfs_doregs(curp, l, pfs, uio));
256
257 case Pfpregs:
258 return (procfs_dofpregs(curp, l, pfs, uio));
259
260 case Pctl:
261 return (procfs_doctl(curp, l, pfs, uio));
262
263 case Pstatus:
264 return (procfs_dostatus(curp, l, pfs, uio));
265
266 case Pmap:
267 return (procfs_domap(curp, p, pfs, uio, 0));
268
269 case Pmaps:
270 return (procfs_domap(curp, p, pfs, uio, 1));
271
272 case Pmem:
273 return (procfs_domem(curp, l, pfs, uio));
274
275 case Pcmdline:
276 return (procfs_docmdline(curp, p, pfs, uio));
277
278 case Pmeminfo:
279 return (procfs_domeminfo(curp, p, pfs, uio));
280 case Pcpuinfo:
281 return (procfs_docpuinfo(curp, p, pfs, uio));
282
283 default:
284 return (EOPNOTSUPP);
285 }
286 }
287
288 /*
289 * Get a string from userland into (buf). Strip a trailing
290 * nl character (to allow easy access from the shell).
291 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
292 * will automatically add a nul char at the end.
293 *
294 * Returns 0 on success or the following errors
295 *
296 * EINVAL: file offset is non-zero.
297 * EMSGSIZE: message is longer than kernel buffer
298 * EFAULT: user i/o buffer is not addressable
299 */
300 int
301 vfs_getuserstr(uio, buf, buflenp)
302 struct uio *uio;
303 char *buf;
304 int *buflenp;
305 {
306 int xlen;
307 int error;
308
309 if (uio->uio_offset != 0)
310 return (EINVAL);
311
312 xlen = *buflenp;
313
314 /* must be able to read the whole string in one go */
315 if (xlen < uio->uio_resid)
316 return (EMSGSIZE);
317 xlen = uio->uio_resid;
318
319 if ((error = uiomove(buf, xlen, uio)) != 0)
320 return (error);
321
322 /* allow multiple writes without seeks */
323 uio->uio_offset = 0;
324
325 /* cleanup string and remove trailing newline */
326 buf[xlen] = '\0';
327 xlen = strlen(buf);
328 if (xlen > 0 && buf[xlen-1] == '\n')
329 buf[--xlen] = '\0';
330 *buflenp = xlen;
331
332 return (0);
333 }
334
335 const vfs_namemap_t *
336 vfs_findname(nm, buf, buflen)
337 const vfs_namemap_t *nm;
338 const char *buf;
339 int buflen;
340 {
341
342 for (; nm->nm_name; nm++)
343 if (memcmp(buf, nm->nm_name, buflen+1) == 0)
344 return (nm);
345
346 return (0);
347 }
348
349 /*
350 * Initialize pfsnode hash table.
351 */
352 void
353 procfs_hashinit()
354 {
355 lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
356 pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
357 M_WAITOK, &pfs_ihash);
358 simple_lock_init(&pfs_hash_slock);
359 }
360
361 /*
362 * Free pfsnode hash table.
363 */
364 void
365 procfs_hashdone()
366 {
367 hashdone(pfs_hashtbl, M_UFSMNT);
368 }
369
370 struct vnode *
371 procfs_hashget(pid, type, mp)
372 pid_t pid;
373 pfstype type;
374 struct mount *mp;
375 {
376 struct pfsnode *pp;
377 struct vnode *vp;
378
379 loop:
380 simple_lock(&pfs_hash_slock);
381 for (pp = PFSPIDHASH(pid)->lh_first; pp; pp = pp->pfs_hash.le_next) {
382 vp = PFSTOV(pp);
383 if (pid == pp->pfs_pid && pp->pfs_type == type &&
384 vp->v_mount == mp) {
385 simple_lock(&vp->v_interlock);
386 simple_unlock(&pfs_hash_slock);
387 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
388 goto loop;
389 return (vp);
390 }
391 }
392 simple_unlock(&pfs_hash_slock);
393 return (NULL);
394 }
395
396 /*
397 * Insert the pfsnode into the hash table and lock it.
398 */
399 void
400 procfs_hashins(pp)
401 struct pfsnode *pp;
402 {
403 struct pfs_hashhead *ppp;
404
405 /* lock the pfsnode, then put it on the appropriate hash list */
406 lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
407
408 simple_lock(&pfs_hash_slock);
409 ppp = PFSPIDHASH(pp->pfs_pid);
410 LIST_INSERT_HEAD(ppp, pp, pfs_hash);
411 simple_unlock(&pfs_hash_slock);
412 }
413
414 /*
415 * Remove the pfsnode from the hash table.
416 */
417 void
418 procfs_hashrem(pp)
419 struct pfsnode *pp;
420 {
421 simple_lock(&pfs_hash_slock);
422 LIST_REMOVE(pp, pfs_hash);
423 simple_unlock(&pfs_hash_slock);
424 }
425
426 void
427 procfs_revoke_vnodes(p, arg)
428 struct proc *p;
429 void *arg;
430 {
431 struct pfsnode *pfs, *pnext;
432 struct vnode *vp;
433 struct mount *mp = (struct mount *)arg;
434
435 if (!(p->p_flag & P_SUGID))
436 return;
437
438 for (pfs = PFSPIDHASH(p->p_pid)->lh_first; pfs; pfs = pnext) {
439 vp = PFSTOV(pfs);
440 pnext = pfs->pfs_hash.le_next;
441 if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
442 vp->v_mount == mp)
443 VOP_REVOKE(vp, REVOKEALL);
444 }
445 }
446