procfs_subr.c revision 1.74 1 /* $NetBSD: procfs_subr.c,v 1.74 2006/12/24 17:37:35 christos Exp $ */
2
3 /*
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
35 */
36
37 /*
38 * Copyright (c) 1994 Christopher G. Demetriou. All rights reserved.
39 * Copyright (c) 1993 Jan-Simon Pendry
40 *
41 * This code is derived from software contributed to Berkeley by
42 * Jan-Simon Pendry.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * @(#)procfs_subr.c 8.6 (Berkeley) 5/14/95
73 */
74
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: procfs_subr.c,v 1.74 2006/12/24 17:37:35 christos Exp $");
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/time.h>
81 #include <sys/kernel.h>
82 #include <sys/proc.h>
83 #include <sys/vnode.h>
84 #include <sys/malloc.h>
85 #include <sys/stat.h>
86 #include <sys/file.h>
87 #include <sys/filedesc.h>
88 #include <sys/kauth.h>
89
90 #include <miscfs/procfs/procfs.h>
91
92 void procfs_hashins(struct pfsnode *);
93 void procfs_hashrem(struct pfsnode *);
94 struct vnode *procfs_hashget(pid_t, pfstype, int, struct mount *);
95
96 LIST_HEAD(pfs_hashhead, pfsnode) *pfs_hashtbl;
97 u_long pfs_ihash; /* size of hash table - 1 */
98 #define PFSPIDHASH(pid) ((pid) & pfs_ihash)
99
100 struct lock pfs_hashlock;
101 struct simplelock pfs_hash_slock;
102
103 #define ISSET(t, f) ((t) & (f))
104
105 /*
106 * allocate a pfsnode/vnode pair. the vnode is
107 * referenced, and locked.
108 *
109 * the pid, pfs_type, and mount point uniquely
110 * identify a pfsnode. the mount point is needed
111 * because someone might mount this filesystem
112 * twice.
113 *
114 * all pfsnodes are maintained on a singly-linked
115 * list. new nodes are only allocated when they cannot
116 * be found on this list. entries on the list are
117 * removed when the vfs reclaim entry is called.
118 *
119 * a single lock is kept for the entire list. this is
120 * needed because the getnewvnode() function can block
121 * waiting for a vnode to become free, in which case there
122 * may be more than one process trying to get the same
123 * vnode. this lock is only taken if we are going to
124 * call getnewvnode, since the kernel itself is single-threaded.
125 *
126 * if an entry is found on the list, then call vget() to
127 * take a reference. this is done because there may be
128 * zero references to it and so it needs to removed from
129 * the vnode free list.
130 */
131 int
132 procfs_allocvp(mp, vpp, pid, pfs_type, fd)
133 struct mount *mp;
134 struct vnode **vpp;
135 pid_t pid;
136 pfstype pfs_type;
137 int fd;
138 {
139 struct pfsnode *pfs;
140 struct vnode *vp;
141 int error;
142
143 do {
144 if ((*vpp = procfs_hashget(pid, pfs_type, fd, mp)) != NULL)
145 return (0);
146 } while (lockmgr(&pfs_hashlock, LK_EXCLUSIVE|LK_SLEEPFAIL, 0));
147
148 if ((error = getnewvnode(VT_PROCFS, mp, procfs_vnodeop_p, &vp)) != 0) {
149 *vpp = NULL;
150 lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
151 return (error);
152 }
153
154 MALLOC(pfs, void *, sizeof(struct pfsnode), M_TEMP, M_WAITOK);
155 vp->v_data = pfs;
156
157 pfs->pfs_pid = pid;
158 pfs->pfs_type = pfs_type;
159 pfs->pfs_vnode = vp;
160 pfs->pfs_flags = 0;
161 pfs->pfs_fileno = PROCFS_FILENO(pid, pfs_type, fd);
162 pfs->pfs_fd = fd;
163
164 switch (pfs_type) {
165 case PFSroot: /* /proc = dr-xr-xr-x */
166 vp->v_flag = VROOT;
167 /*FALLTHROUGH*/
168 case PFSproc: /* /proc/N = dr-xr-xr-x */
169 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
170 vp->v_type = VDIR;
171 break;
172
173 case PFScurproc: /* /proc/curproc = lr-xr-xr-x */
174 case PFSself: /* /proc/self = lr-xr-xr-x */
175 case PFScwd: /* /proc/N/cwd = lr-xr-xr-x */
176 case PFSchroot: /* /proc/N/chroot = lr-xr-xr-x */
177 case PFSexe: /* /proc/N/exe = lr-xr-xr-x */
178 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
179 vp->v_type = VLNK;
180 break;
181
182 case PFSfd:
183 if (fd == -1) { /* /proc/N/fd = dr-xr-xr-x */
184 pfs->pfs_mode = S_IRUSR|S_IXUSR;
185 vp->v_type = VDIR;
186 } else { /* /proc/N/fd/M = [ps-]rw------- */
187 struct file *fp;
188 struct vnode *vxp;
189 struct proc *pown;
190
191 /* XXX can procfs_getfp() ever fail here? */
192 if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
193 goto bad;
194 FILE_USE(fp);
195
196 pfs->pfs_mode = S_IRUSR|S_IWUSR;
197 switch (fp->f_type) {
198 case DTYPE_VNODE:
199 vxp = (struct vnode *)fp->f_data;
200
201 /*
202 * We make symlinks for directories
203 * to avoid cycles.
204 */
205 if (vxp->v_type == VDIR)
206 goto symlink;
207 vp->v_type = vxp->v_type;
208 break;
209 case DTYPE_PIPE:
210 vp->v_type = VFIFO;
211 break;
212 case DTYPE_SOCKET:
213 vp->v_type = VSOCK;
214 break;
215 case DTYPE_KQUEUE:
216 case DTYPE_MISC:
217 symlink:
218 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|
219 S_IXGRP|S_IROTH|S_IXOTH;
220 vp->v_type = VLNK;
221 break;
222 default:
223 error = EOPNOTSUPP;
224 FILE_UNUSE(fp, proc_representative_lwp(pown));
225 goto bad;
226 }
227 FILE_UNUSE(fp, proc_representative_lwp(pown));
228 }
229 break;
230
231 case PFSfile: /* /proc/N/file = -rw------- */
232 case PFSmem: /* /proc/N/mem = -rw------- */
233 case PFSregs: /* /proc/N/regs = -rw------- */
234 case PFSfpregs: /* /proc/N/fpregs = -rw------- */
235 pfs->pfs_mode = S_IRUSR|S_IWUSR;
236 vp->v_type = VREG;
237 break;
238
239 case PFSctl: /* /proc/N/ctl = --w------ */
240 case PFSnote: /* /proc/N/note = --w------ */
241 case PFSnotepg: /* /proc/N/notepg = --w------ */
242 pfs->pfs_mode = S_IWUSR;
243 vp->v_type = VREG;
244 break;
245
246 case PFSmap: /* /proc/N/map = -r--r--r-- */
247 case PFSmaps: /* /proc/N/maps = -r--r--r-- */
248 case PFSstatus: /* /proc/N/status = -r--r--r-- */
249 case PFSstat: /* /proc/N/stat = -r--r--r-- */
250 case PFScmdline: /* /proc/N/cmdline = -r--r--r-- */
251 case PFSemul: /* /proc/N/emul = -r--r--r-- */
252 case PFSmeminfo: /* /proc/meminfo = -r--r--r-- */
253 case PFSdevices: /* /proc/devices = -r--r--r-- */
254 case PFScpuinfo: /* /proc/cpuinfo = -r--r--r-- */
255 case PFSuptime: /* /proc/uptime = -r--r--r-- */
256 case PFSmounts: /* /proc/mounts = -r--r--r-- */
257 pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
258 vp->v_type = VREG;
259 break;
260
261 #ifdef __HAVE_PROCFS_MACHDEP
262 PROCFS_MACHDEP_NODETYPE_CASES
263 procfs_machdep_allocvp(vp);
264 break;
265 #endif
266
267 default:
268 panic("procfs_allocvp");
269 }
270
271 procfs_hashins(pfs);
272 uvm_vnp_setsize(vp, 0);
273 lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
274
275 *vpp = vp;
276 return (0);
277
278 bad:
279 lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
280 FREE(pfs, M_TEMP);
281 ungetnewvnode(vp);
282 return (error);
283 }
284
285 int
286 procfs_freevp(vp)
287 struct vnode *vp;
288 {
289 struct pfsnode *pfs = VTOPFS(vp);
290
291 procfs_hashrem(pfs);
292
293 FREE(vp->v_data, M_TEMP);
294 vp->v_data = 0;
295 return (0);
296 }
297
298 int
299 procfs_rw(v)
300 void *v;
301 {
302 struct vop_read_args *ap = v;
303 struct vnode *vp = ap->a_vp;
304 struct uio *uio = ap->a_uio;
305 struct lwp *curl;
306 struct lwp *l;
307 struct pfsnode *pfs = VTOPFS(vp);
308 struct proc *p;
309 int error;
310
311 if (uio->uio_offset < 0)
312 return EINVAL;
313 p = PFIND(pfs->pfs_pid);
314 if (p == 0)
315 return ESRCH;
316
317 if (ISSET(p->p_flag, P_INEXEC))
318 return (EAGAIN);
319
320 curl = curlwp;
321
322 /*
323 * Do not allow init to be modified while in secure mode; it
324 * could be duped into changing the security level.
325 */
326 #define M2K(m) ((m) == UIO_READ ? KAUTH_REQ_PROCESS_CANPROCFS_READ : \
327 KAUTH_REQ_PROCESS_CANPROCFS_WRITE)
328 error = kauth_authorize_process(curl->l_cred, KAUTH_PROCESS_CANPROCFS,
329 p, pfs, KAUTH_ARG(M2K(uio->uio_rw)), NULL);
330 if (error)
331 return (error);
332 #undef M2K
333
334 /* XXX NJWLWP
335 * The entire procfs interface needs work to be useful to
336 * a process with multiple LWPs. For the moment, we'll
337 * just kluge this and fail on others.
338 */
339 l = proc_representative_lwp(p);
340
341 switch (pfs->pfs_type) {
342 case PFSnote:
343 case PFSnotepg:
344 return (procfs_donote(curl, p, pfs, uio));
345
346 case PFSregs:
347 return (procfs_doregs(curl, l, pfs, uio));
348
349 case PFSfpregs:
350 return (procfs_dofpregs(curl, l, pfs, uio));
351
352 case PFSctl:
353 return (procfs_doctl(curl, l, pfs, uio));
354
355 case PFSstatus:
356 return (procfs_dostatus(curl, l, pfs, uio));
357
358 case PFSstat:
359 return (procfs_do_pid_stat(curl, l, pfs, uio));
360
361 case PFSmap:
362 return (procfs_domap(curl, p, pfs, uio, 0));
363
364 case PFSmaps:
365 return (procfs_domap(curl, p, pfs, uio, 1));
366
367 case PFSmem:
368 return (procfs_domem(curl, l, pfs, uio));
369
370 case PFScmdline:
371 return (procfs_docmdline(curl, p, pfs, uio));
372
373 case PFSmeminfo:
374 return (procfs_domeminfo(curl, p, pfs, uio));
375
376 case PFSdevices:
377 return (procfs_dodevices(curl, p, pfs, uio));
378
379 case PFScpuinfo:
380 return (procfs_docpuinfo(curl, p, pfs, uio));
381
382 case PFSfd:
383 return (procfs_dofd(curl, p, pfs, uio));
384
385 case PFSuptime:
386 return (procfs_douptime(curl, p, pfs, uio));
387
388 case PFSmounts:
389 return (procfs_domounts(curl, p, pfs, uio));
390
391 case PFSemul:
392 return procfs_doemul(curl, p, pfs, uio);
393
394 #ifdef __HAVE_PROCFS_MACHDEP
395 PROCFS_MACHDEP_NODETYPE_CASES
396 return (procfs_machdep_rw(curl, l, pfs, uio));
397 #endif
398
399 default:
400 return (EOPNOTSUPP);
401 }
402 }
403
404 /*
405 * Get a string from userland into (bf). Strip a trailing
406 * nl character (to allow easy access from the shell).
407 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
408 * will automatically add a nul char at the end.
409 *
410 * Returns 0 on success or the following errors
411 *
412 * EINVAL: file offset is non-zero.
413 * EMSGSIZE: message is longer than kernel buffer
414 * EFAULT: user i/o buffer is not addressable
415 */
416 int
417 vfs_getuserstr(uio, bf, buflenp)
418 struct uio *uio;
419 char *bf;
420 int *buflenp;
421 {
422 int xlen;
423 int error;
424
425 if (uio->uio_offset != 0)
426 return (EINVAL);
427
428 xlen = *buflenp;
429
430 /* must be able to read the whole string in one go */
431 if (xlen < uio->uio_resid)
432 return (EMSGSIZE);
433 xlen = uio->uio_resid;
434
435 if ((error = uiomove(bf, xlen, uio)) != 0)
436 return (error);
437
438 /* allow multiple writes without seeks */
439 uio->uio_offset = 0;
440
441 /* cleanup string and remove trailing newline */
442 bf[xlen] = '\0';
443 xlen = strlen(bf);
444 if (xlen > 0 && bf[xlen-1] == '\n')
445 bf[--xlen] = '\0';
446 *buflenp = xlen;
447
448 return (0);
449 }
450
451 const vfs_namemap_t *
452 vfs_findname(nm, bf, buflen)
453 const vfs_namemap_t *nm;
454 const char *bf;
455 int buflen;
456 {
457
458 for (; nm->nm_name; nm++)
459 if (memcmp(bf, nm->nm_name, buflen+1) == 0)
460 return (nm);
461
462 return (0);
463 }
464
465 /*
466 * Initialize pfsnode hash table.
467 */
468 void
469 procfs_hashinit()
470 {
471 lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
472 pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
473 M_WAITOK, &pfs_ihash);
474 simple_lock_init(&pfs_hash_slock);
475 }
476
477 void
478 procfs_hashreinit()
479 {
480 struct pfsnode *pp;
481 struct pfs_hashhead *oldhash, *hash;
482 u_long i, oldmask, mask, val;
483
484 hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
485 &mask);
486
487 simple_lock(&pfs_hash_slock);
488 oldhash = pfs_hashtbl;
489 oldmask = pfs_ihash;
490 pfs_hashtbl = hash;
491 pfs_ihash = mask;
492 for (i = 0; i <= oldmask; i++) {
493 while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
494 LIST_REMOVE(pp, pfs_hash);
495 val = PFSPIDHASH(pp->pfs_pid);
496 LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
497 }
498 }
499 simple_unlock(&pfs_hash_slock);
500 hashdone(oldhash, M_UFSMNT);
501 }
502
503 /*
504 * Free pfsnode hash table.
505 */
506 void
507 procfs_hashdone()
508 {
509 hashdone(pfs_hashtbl, M_UFSMNT);
510 }
511
512 struct vnode *
513 procfs_hashget(pid, type, fd, mp)
514 pid_t pid;
515 pfstype type;
516 int fd;
517 struct mount *mp;
518 {
519 struct pfs_hashhead *ppp;
520 struct pfsnode *pp;
521 struct vnode *vp;
522
523 loop:
524 simple_lock(&pfs_hash_slock);
525 ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
526 LIST_FOREACH(pp, ppp, pfs_hash) {
527 vp = PFSTOV(pp);
528 if (pid == pp->pfs_pid && pp->pfs_type == type &&
529 pp->pfs_fd == fd && vp->v_mount == mp) {
530 simple_lock(&vp->v_interlock);
531 simple_unlock(&pfs_hash_slock);
532 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
533 goto loop;
534 return (vp);
535 }
536 }
537 simple_unlock(&pfs_hash_slock);
538 return (NULL);
539 }
540
541 /*
542 * Insert the pfsnode into the hash table and lock it.
543 */
544 void
545 procfs_hashins(pp)
546 struct pfsnode *pp;
547 {
548 struct pfs_hashhead *ppp;
549
550 /* lock the pfsnode, then put it on the appropriate hash list */
551 lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
552
553 simple_lock(&pfs_hash_slock);
554 ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
555 LIST_INSERT_HEAD(ppp, pp, pfs_hash);
556 simple_unlock(&pfs_hash_slock);
557 }
558
559 /*
560 * Remove the pfsnode from the hash table.
561 */
562 void
563 procfs_hashrem(pp)
564 struct pfsnode *pp;
565 {
566 simple_lock(&pfs_hash_slock);
567 LIST_REMOVE(pp, pfs_hash);
568 simple_unlock(&pfs_hash_slock);
569 }
570
571 void
572 procfs_revoke_vnodes(p, arg)
573 struct proc *p;
574 void *arg;
575 {
576 struct pfsnode *pfs, *pnext;
577 struct vnode *vp;
578 struct mount *mp = (struct mount *)arg;
579 struct pfs_hashhead *ppp;
580
581 if (!(p->p_flag & P_SUGID))
582 return;
583
584 ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
585 for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
586 vp = PFSTOV(pfs);
587 pnext = LIST_NEXT(pfs, pfs_hash);
588 if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
589 vp->v_mount == mp)
590 VOP_REVOKE(vp, REVOKEALL);
591 }
592 }
593
594 int
595 procfs_getfp(pfs, pown, fp)
596 struct pfsnode *pfs;
597 struct proc **pown;
598 struct file **fp;
599 {
600 struct proc *p = PFIND(pfs->pfs_pid);
601
602 if (p == NULL)
603 return ESRCH;
604
605 if (pfs->pfs_fd == -1)
606 return EINVAL;
607
608 if ((*fp = fd_getfile(p->p_fd, pfs->pfs_fd)) == NULL)
609 return EBADF;
610
611 *pown = p;
612 return 0;
613 }
614
615 int
616 procfs_doemul(struct lwp *curl, struct proc *p,
617 struct pfsnode *pfs, struct uio *uio)
618 {
619 const char *ename = p->p_emul->e_name;
620 return uiomove_frombuf(__UNCONST(ename), strlen(ename), uio);
621 }
622