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