procfs_subr.c revision 1.41 1 /* $NetBSD: procfs_subr.c,v 1.41 2002/11/07 08:21:36 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/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.41 2002/11/07 08:21:36 thorpej 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 #ifdef __HAVE_PROCFS_MACHDEP
172 PROCFS_MACHDEP_NODETYPE_CASES
173 procfs_machdep_allocvp(vp);
174 break;
175 #endif
176
177 default:
178 panic("procfs_allocvp");
179 }
180
181 procfs_hashins(pfs);
182 uvm_vnp_setsize(vp, 0);
183 lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
184
185 return (error);
186 }
187
188 int
189 procfs_freevp(vp)
190 struct vnode *vp;
191 {
192 struct pfsnode *pfs = VTOPFS(vp);
193
194 procfs_hashrem(pfs);
195
196 FREE(vp->v_data, M_TEMP);
197 vp->v_data = 0;
198 return (0);
199 }
200
201 int
202 procfs_rw(v)
203 void *v;
204 {
205 struct vop_read_args *ap = v;
206 struct vnode *vp = ap->a_vp;
207 struct uio *uio = ap->a_uio;
208 struct proc *curp = uio->uio_procp;
209 struct pfsnode *pfs = VTOPFS(vp);
210 struct proc *p;
211
212 p = PFIND(pfs->pfs_pid);
213 if (p == 0)
214 return (EINVAL);
215
216 switch (pfs->pfs_type) {
217 case Pregs:
218 case Pfpregs:
219 case Pmem:
220 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
221 PROCFS_MACHDEP_PROTECT_CASES
222 #endif
223 /*
224 * Do not allow init to be modified while in secure mode; it
225 * could be duped into changing the security level.
226 */
227 if (uio->uio_rw == UIO_WRITE &&
228 p == initproc && securelevel > -1)
229 return (EPERM);
230 break;
231
232 default:
233 break;
234 }
235
236 switch (pfs->pfs_type) {
237 case Pnote:
238 case Pnotepg:
239 return (procfs_donote(curp, p, pfs, uio));
240
241 case Pregs:
242 return (procfs_doregs(curp, p, pfs, uio));
243
244 case Pfpregs:
245 return (procfs_dofpregs(curp, p, pfs, uio));
246
247 case Pctl:
248 return (procfs_doctl(curp, p, pfs, uio));
249
250 case Pstatus:
251 return (procfs_dostatus(curp, p, pfs, uio));
252
253 case Pmap:
254 return (procfs_domap(curp, p, pfs, uio, 0));
255
256 case Pmaps:
257 return (procfs_domap(curp, p, pfs, uio, 1));
258
259 case Pmem:
260 return (procfs_domem(curp, p, pfs, uio));
261
262 case Pcmdline:
263 return (procfs_docmdline(curp, p, pfs, uio));
264
265 case Pmeminfo:
266 return (procfs_domeminfo(curp, p, pfs, uio));
267
268 case Pcpuinfo:
269 return (procfs_docpuinfo(curp, p, pfs, uio));
270
271 #ifdef __HAVE_PROCFS_MACHDEP
272 PROCFS_MACHDEP_NODETYPE_CASES
273 return (procfs_machdep_rw(curp, p, pfs, uio));
274 #endif
275
276 default:
277 return (EOPNOTSUPP);
278 }
279 }
280
281 /*
282 * Get a string from userland into (buf). Strip a trailing
283 * nl character (to allow easy access from the shell).
284 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
285 * will automatically add a nul char at the end.
286 *
287 * Returns 0 on success or the following errors
288 *
289 * EINVAL: file offset is non-zero.
290 * EMSGSIZE: message is longer than kernel buffer
291 * EFAULT: user i/o buffer is not addressable
292 */
293 int
294 vfs_getuserstr(uio, buf, buflenp)
295 struct uio *uio;
296 char *buf;
297 int *buflenp;
298 {
299 int xlen;
300 int error;
301
302 if (uio->uio_offset != 0)
303 return (EINVAL);
304
305 xlen = *buflenp;
306
307 /* must be able to read the whole string in one go */
308 if (xlen < uio->uio_resid)
309 return (EMSGSIZE);
310 xlen = uio->uio_resid;
311
312 if ((error = uiomove(buf, xlen, uio)) != 0)
313 return (error);
314
315 /* allow multiple writes without seeks */
316 uio->uio_offset = 0;
317
318 /* cleanup string and remove trailing newline */
319 buf[xlen] = '\0';
320 xlen = strlen(buf);
321 if (xlen > 0 && buf[xlen-1] == '\n')
322 buf[--xlen] = '\0';
323 *buflenp = xlen;
324
325 return (0);
326 }
327
328 const vfs_namemap_t *
329 vfs_findname(nm, buf, buflen)
330 const vfs_namemap_t *nm;
331 const char *buf;
332 int buflen;
333 {
334
335 for (; nm->nm_name; nm++)
336 if (memcmp(buf, nm->nm_name, buflen+1) == 0)
337 return (nm);
338
339 return (0);
340 }
341
342 /*
343 * Initialize pfsnode hash table.
344 */
345 void
346 procfs_hashinit()
347 {
348 lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
349 pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
350 M_WAITOK, &pfs_ihash);
351 simple_lock_init(&pfs_hash_slock);
352 }
353
354 void
355 procfs_hashreinit()
356 {
357 struct pfsnode *pp;
358 struct pfs_hashhead *oldhash, *hash;
359 u_long i, oldmask, mask, val;
360
361 hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
362 &mask);
363
364 simple_lock(&pfs_hash_slock);
365 oldhash = pfs_hashtbl;
366 oldmask = pfs_ihash;
367 pfs_hashtbl = hash;
368 pfs_ihash = mask;
369 for (i = 0; i <= oldmask; i++) {
370 while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
371 LIST_REMOVE(pp, pfs_hash);
372 val = PFSPIDHASH(pp->pfs_pid);
373 LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
374 }
375 }
376 simple_unlock(&pfs_hash_slock);
377 hashdone(oldhash, M_UFSMNT);
378 }
379
380 /*
381 * Free pfsnode hash table.
382 */
383 void
384 procfs_hashdone()
385 {
386 hashdone(pfs_hashtbl, M_UFSMNT);
387 }
388
389 struct vnode *
390 procfs_hashget(pid, type, mp)
391 pid_t pid;
392 pfstype type;
393 struct mount *mp;
394 {
395 struct pfs_hashhead *ppp;
396 struct pfsnode *pp;
397 struct vnode *vp;
398
399 loop:
400 simple_lock(&pfs_hash_slock);
401 ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
402 LIST_FOREACH(pp, ppp, pfs_hash) {
403 vp = PFSTOV(pp);
404 if (pid == pp->pfs_pid && pp->pfs_type == type &&
405 vp->v_mount == mp) {
406 simple_lock(&vp->v_interlock);
407 simple_unlock(&pfs_hash_slock);
408 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
409 goto loop;
410 return (vp);
411 }
412 }
413 simple_unlock(&pfs_hash_slock);
414 return (NULL);
415 }
416
417 /*
418 * Insert the pfsnode into the hash table and lock it.
419 */
420 void
421 procfs_hashins(pp)
422 struct pfsnode *pp;
423 {
424 struct pfs_hashhead *ppp;
425
426 /* lock the pfsnode, then put it on the appropriate hash list */
427 lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
428
429 simple_lock(&pfs_hash_slock);
430 ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
431 LIST_INSERT_HEAD(ppp, pp, pfs_hash);
432 simple_unlock(&pfs_hash_slock);
433 }
434
435 /*
436 * Remove the pfsnode from the hash table.
437 */
438 void
439 procfs_hashrem(pp)
440 struct pfsnode *pp;
441 {
442 simple_lock(&pfs_hash_slock);
443 LIST_REMOVE(pp, pfs_hash);
444 simple_unlock(&pfs_hash_slock);
445 }
446
447 void
448 procfs_revoke_vnodes(p, arg)
449 struct proc *p;
450 void *arg;
451 {
452 struct pfsnode *pfs, *pnext;
453 struct vnode *vp;
454 struct mount *mp = (struct mount *)arg;
455 struct pfs_hashhead *ppp;
456
457 if (!(p->p_flag & P_SUGID))
458 return;
459
460 ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
461 for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
462 vp = PFSTOV(pfs);
463 pnext = LIST_NEXT(pfs, pfs_hash);
464 if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
465 vp->v_mount == mp)
466 VOP_REVOKE(vp, REVOKEALL);
467 }
468 }
469