procfs_subr.c revision 1.50 1 /* $NetBSD: procfs_subr.c,v 1.50 2003/04/17 20:50:46 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.50 2003/04/17 20:50:46 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 struct proc *pown;
154
155 /* XXX can procfs_getfp() ever fail here? */
156 if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
157 goto bad;
158 FILE_USE(fp);
159
160 pfs->pfs_mode = S_IRUSR|S_IWUSR;
161 switch (fp->f_type) {
162 case DTYPE_VNODE:
163 vxp = (struct vnode *)fp->f_data;
164
165 /* Do not allow opening directories */
166 if (vxp->v_type == VDIR) {
167 error = EOPNOTSUPP;
168 FILE_UNUSE(fp, pown);
169 goto bad;
170 }
171
172 vp->v_type = vxp->v_type;
173 break;
174 case DTYPE_PIPE:
175 vp->v_type = VFIFO;
176 break;
177 case DTYPE_SOCKET:
178 vp->v_type = VSOCK;
179 break;
180 default:
181 error = EOPNOTSUPP;
182 FILE_UNUSE(fp, pown);
183 goto bad;
184 }
185 FILE_UNUSE(fp, pown);
186 }
187 break;
188
189 case Pfile: /* /proc/N/file = -rw------- */
190 case Pmem: /* /proc/N/mem = -rw------- */
191 case Pregs: /* /proc/N/regs = -rw------- */
192 case Pfpregs: /* /proc/N/fpregs = -rw------- */
193 pfs->pfs_mode = S_IRUSR|S_IWUSR;
194 vp->v_type = VREG;
195 break;
196
197 case Pctl: /* /proc/N/ctl = --w------ */
198 case Pnote: /* /proc/N/note = --w------ */
199 case Pnotepg: /* /proc/N/notepg = --w------ */
200 pfs->pfs_mode = S_IWUSR;
201 vp->v_type = VREG;
202 break;
203
204 case Pmap: /* /proc/N/map = -r--r--r-- */
205 case Pmaps: /* /proc/N/maps = -r--r--r-- */
206 case Pstatus: /* /proc/N/status = -r--r--r-- */
207 case Pcmdline: /* /proc/N/cmdline = -r--r--r-- */
208 case Pmeminfo: /* /proc/meminfo = -r--r--r-- */
209 case Pcpuinfo: /* /proc/cpuinfo = -r--r--r-- */
210 case Puptime: /* /proc/uptime = -r--r--r-- */
211 pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
212 vp->v_type = VREG;
213 break;
214
215 #ifdef __HAVE_PROCFS_MACHDEP
216 PROCFS_MACHDEP_NODETYPE_CASES
217 procfs_machdep_allocvp(vp);
218 break;
219 #endif
220
221 default:
222 panic("procfs_allocvp");
223 }
224
225 procfs_hashins(pfs);
226 uvm_vnp_setsize(vp, 0);
227 lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
228
229 *vpp = vp;
230 return (0);
231
232 bad:
233 lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
234 FREE(pfs, M_TEMP);
235 ungetnewvnode(vp);
236 return (error);
237 }
238
239 int
240 procfs_freevp(vp)
241 struct vnode *vp;
242 {
243 struct pfsnode *pfs = VTOPFS(vp);
244
245 procfs_hashrem(pfs);
246
247 FREE(vp->v_data, M_TEMP);
248 vp->v_data = 0;
249 return (0);
250 }
251
252 int
253 procfs_rw(v)
254 void *v;
255 {
256 struct vop_read_args *ap = v;
257 struct vnode *vp = ap->a_vp;
258 struct uio *uio = ap->a_uio;
259 struct proc *curp = uio->uio_procp;
260 struct pfsnode *pfs = VTOPFS(vp);
261 struct lwp *l;
262 struct proc *p;
263
264 p = PFIND(pfs->pfs_pid);
265 if (p == 0)
266 return (EINVAL);
267
268 /* XXX NJWLWP
269 * The entire procfs interface needs work to be useful to
270 * a process with multiple LWPs. For the moment, we'll
271 * just kluge this and fail on others.
272 */
273 l = proc_representative_lwp(p);
274
275 switch (pfs->pfs_type) {
276 case Pregs:
277 case Pfpregs:
278 case Pmem:
279 #if defined(__HAVE_PROCFS_MACHDEP) && defined(PROCFS_MACHDEP_PROTECT_CASES)
280 PROCFS_MACHDEP_PROTECT_CASES
281 #endif
282 /*
283 * Do not allow init to be modified while in secure mode; it
284 * could be duped into changing the security level.
285 */
286 if (uio->uio_rw == UIO_WRITE &&
287 p == initproc && securelevel > -1)
288 return (EPERM);
289 break;
290
291 default:
292 break;
293 }
294
295 switch (pfs->pfs_type) {
296 case Pnote:
297 case Pnotepg:
298 return (procfs_donote(curp, p, pfs, uio));
299
300 case Pregs:
301 return (procfs_doregs(curp, l, pfs, uio));
302
303 case Pfpregs:
304 return (procfs_dofpregs(curp, l, pfs, uio));
305
306 case Pctl:
307 return (procfs_doctl(curp, l, pfs, uio));
308
309 case Pstatus:
310 return (procfs_dostatus(curp, l, pfs, uio));
311
312 case Pmap:
313 return (procfs_domap(curp, p, pfs, uio, 0));
314
315 case Pmaps:
316 return (procfs_domap(curp, p, pfs, uio, 1));
317
318 case Pmem:
319 return (procfs_domem(curp, p, pfs, uio));
320
321 case Pcmdline:
322 return (procfs_docmdline(curp, p, pfs, uio));
323
324 case Pmeminfo:
325 return (procfs_domeminfo(curp, p, pfs, uio));
326
327 case Pcpuinfo:
328 return (procfs_docpuinfo(curp, p, pfs, uio));
329
330 case Pfd:
331 return (procfs_dofd(curp, p, pfs, uio));
332
333 case Puptime:
334 return (procfs_douptime(curp, p, pfs, uio));
335
336 #ifdef __HAVE_PROCFS_MACHDEP
337 PROCFS_MACHDEP_NODETYPE_CASES
338 return (procfs_machdep_rw(curp, l, pfs, uio));
339 #endif
340
341 default:
342 return (EOPNOTSUPP);
343 }
344 }
345
346 /*
347 * Get a string from userland into (buf). Strip a trailing
348 * nl character (to allow easy access from the shell).
349 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
350 * will automatically add a nul char at the end.
351 *
352 * Returns 0 on success or the following errors
353 *
354 * EINVAL: file offset is non-zero.
355 * EMSGSIZE: message is longer than kernel buffer
356 * EFAULT: user i/o buffer is not addressable
357 */
358 int
359 vfs_getuserstr(uio, buf, buflenp)
360 struct uio *uio;
361 char *buf;
362 int *buflenp;
363 {
364 int xlen;
365 int error;
366
367 if (uio->uio_offset != 0)
368 return (EINVAL);
369
370 xlen = *buflenp;
371
372 /* must be able to read the whole string in one go */
373 if (xlen < uio->uio_resid)
374 return (EMSGSIZE);
375 xlen = uio->uio_resid;
376
377 if ((error = uiomove(buf, xlen, uio)) != 0)
378 return (error);
379
380 /* allow multiple writes without seeks */
381 uio->uio_offset = 0;
382
383 /* cleanup string and remove trailing newline */
384 buf[xlen] = '\0';
385 xlen = strlen(buf);
386 if (xlen > 0 && buf[xlen-1] == '\n')
387 buf[--xlen] = '\0';
388 *buflenp = xlen;
389
390 return (0);
391 }
392
393 const vfs_namemap_t *
394 vfs_findname(nm, buf, buflen)
395 const vfs_namemap_t *nm;
396 const char *buf;
397 int buflen;
398 {
399
400 for (; nm->nm_name; nm++)
401 if (memcmp(buf, nm->nm_name, buflen+1) == 0)
402 return (nm);
403
404 return (0);
405 }
406
407 /*
408 * Initialize pfsnode hash table.
409 */
410 void
411 procfs_hashinit()
412 {
413 lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
414 pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
415 M_WAITOK, &pfs_ihash);
416 simple_lock_init(&pfs_hash_slock);
417 }
418
419 void
420 procfs_hashreinit()
421 {
422 struct pfsnode *pp;
423 struct pfs_hashhead *oldhash, *hash;
424 u_long i, oldmask, mask, val;
425
426 hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
427 &mask);
428
429 simple_lock(&pfs_hash_slock);
430 oldhash = pfs_hashtbl;
431 oldmask = pfs_ihash;
432 pfs_hashtbl = hash;
433 pfs_ihash = mask;
434 for (i = 0; i <= oldmask; i++) {
435 while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
436 LIST_REMOVE(pp, pfs_hash);
437 val = PFSPIDHASH(pp->pfs_pid);
438 LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
439 }
440 }
441 simple_unlock(&pfs_hash_slock);
442 hashdone(oldhash, M_UFSMNT);
443 }
444
445 /*
446 * Free pfsnode hash table.
447 */
448 void
449 procfs_hashdone()
450 {
451 hashdone(pfs_hashtbl, M_UFSMNT);
452 }
453
454 struct vnode *
455 procfs_hashget(pid, type, fd, mp)
456 pid_t pid;
457 pfstype type;
458 int fd;
459 struct mount *mp;
460 {
461 struct pfs_hashhead *ppp;
462 struct pfsnode *pp;
463 struct vnode *vp;
464
465 loop:
466 simple_lock(&pfs_hash_slock);
467 ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
468 LIST_FOREACH(pp, ppp, pfs_hash) {
469 vp = PFSTOV(pp);
470 if (pid == pp->pfs_pid && pp->pfs_type == type &&
471 pp->pfs_fd == fd && vp->v_mount == mp) {
472 simple_lock(&vp->v_interlock);
473 simple_unlock(&pfs_hash_slock);
474 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
475 goto loop;
476 return (vp);
477 }
478 }
479 simple_unlock(&pfs_hash_slock);
480 return (NULL);
481 }
482
483 /*
484 * Insert the pfsnode into the hash table and lock it.
485 */
486 void
487 procfs_hashins(pp)
488 struct pfsnode *pp;
489 {
490 struct pfs_hashhead *ppp;
491
492 /* lock the pfsnode, then put it on the appropriate hash list */
493 lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
494
495 simple_lock(&pfs_hash_slock);
496 ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
497 LIST_INSERT_HEAD(ppp, pp, pfs_hash);
498 simple_unlock(&pfs_hash_slock);
499 }
500
501 /*
502 * Remove the pfsnode from the hash table.
503 */
504 void
505 procfs_hashrem(pp)
506 struct pfsnode *pp;
507 {
508 simple_lock(&pfs_hash_slock);
509 LIST_REMOVE(pp, pfs_hash);
510 simple_unlock(&pfs_hash_slock);
511 }
512
513 void
514 procfs_revoke_vnodes(p, arg)
515 struct proc *p;
516 void *arg;
517 {
518 struct pfsnode *pfs, *pnext;
519 struct vnode *vp;
520 struct mount *mp = (struct mount *)arg;
521 struct pfs_hashhead *ppp;
522
523 if (!(p->p_flag & P_SUGID))
524 return;
525
526 ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
527 for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
528 vp = PFSTOV(pfs);
529 pnext = LIST_NEXT(pfs, pfs_hash);
530 if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
531 vp->v_mount == mp)
532 VOP_REVOKE(vp, REVOKEALL);
533 }
534 }
535
536 int
537 procfs_getfp(pfs, pown, fp)
538 struct pfsnode *pfs;
539 struct proc **pown;
540 struct file **fp;
541 {
542 struct proc *p = PFIND(pfs->pfs_pid);
543
544 if (p == NULL)
545 return ESRCH;
546
547 if (pfs->pfs_fd == -1)
548 return EINVAL;
549
550 if ((*fp = fd_getfile(p->p_fd, pfs->pfs_fd)) == NULL)
551 return EBADF;
552
553 *pown = p;
554 return 0;
555 }
556