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