procfs_subr.c revision 1.73 1 /* $NetBSD: procfs_subr.c,v 1.73 2006/11/28 17:27:09 elad 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.73 2006/11/28 17:27:09 elad 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 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
167 vp->v_type = VDIR;
168 vp->v_flag = VROOT;
169 break;
170
171 case PFScurproc: /* /proc/curproc = lr-xr-xr-x */
172 case PFSself: /* /proc/self = lr-xr-xr-x */
173 case PFScwd: /* /proc/N/cwd = lr-xr-xr-x */
174 case PFSchroot: /* /proc/N/chroot = lr-xr-xr-x */
175 case PFSexe: /* /proc/N/exe = lr-xr-xr-x */
176 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
177 vp->v_type = VLNK;
178 break;
179
180 case PFSproc: /* /proc/N = dr-xr-xr-x */
181 case PFSfd:
182 if (fd == -1) { /* /proc/N/fd = dr-xr-xr-x */
183 pfs->pfs_mode = S_IRUSR|S_IXUSR;
184 vp->v_type = VDIR;
185 } else { /* /proc/N/fd/M = [ps-]rw------- */
186 struct file *fp;
187 struct vnode *vxp;
188 struct proc *pown;
189
190 /* XXX can procfs_getfp() ever fail here? */
191 if ((error = procfs_getfp(pfs, &pown, &fp)) != 0)
192 goto bad;
193 FILE_USE(fp);
194
195 pfs->pfs_mode = S_IRUSR|S_IWUSR;
196 switch (fp->f_type) {
197 case DTYPE_VNODE:
198 vxp = (struct vnode *)fp->f_data;
199
200 /*
201 * We make symlinks for directories
202 * to avoid cycles.
203 */
204 if (vxp->v_type == VDIR)
205 goto symlink;
206 vp->v_type = vxp->v_type;
207 break;
208 case DTYPE_PIPE:
209 vp->v_type = VFIFO;
210 break;
211 case DTYPE_SOCKET:
212 vp->v_type = VSOCK;
213 break;
214 case DTYPE_KQUEUE:
215 case DTYPE_MISC:
216 symlink:
217 pfs->pfs_mode = S_IRUSR|S_IXUSR|S_IRGRP|
218 S_IXGRP|S_IROTH|S_IXOTH;
219 vp->v_type = VLNK;
220 break;
221 default:
222 error = EOPNOTSUPP;
223 FILE_UNUSE(fp, proc_representative_lwp(pown));
224 goto bad;
225 }
226 FILE_UNUSE(fp, proc_representative_lwp(pown));
227 }
228 break;
229
230 case PFSfile: /* /proc/N/file = -rw------- */
231 case PFSmem: /* /proc/N/mem = -rw------- */
232 case PFSregs: /* /proc/N/regs = -rw------- */
233 case PFSfpregs: /* /proc/N/fpregs = -rw------- */
234 pfs->pfs_mode = S_IRUSR|S_IWUSR;
235 vp->v_type = VREG;
236 break;
237
238 case PFSctl: /* /proc/N/ctl = --w------ */
239 case PFSnote: /* /proc/N/note = --w------ */
240 case PFSnotepg: /* /proc/N/notepg = --w------ */
241 pfs->pfs_mode = S_IWUSR;
242 vp->v_type = VREG;
243 break;
244
245 case PFSmap: /* /proc/N/map = -r--r--r-- */
246 case PFSmaps: /* /proc/N/maps = -r--r--r-- */
247 case PFSstatus: /* /proc/N/status = -r--r--r-- */
248 case PFSstat: /* /proc/N/stat = -r--r--r-- */
249 case PFScmdline: /* /proc/N/cmdline = -r--r--r-- */
250 case PFSemul: /* /proc/N/emul = -r--r--r-- */
251 case PFSmeminfo: /* /proc/meminfo = -r--r--r-- */
252 case PFSdevices: /* /proc/devices = -r--r--r-- */
253 case PFScpuinfo: /* /proc/cpuinfo = -r--r--r-- */
254 case PFSuptime: /* /proc/uptime = -r--r--r-- */
255 case PFSmounts: /* /proc/mounts = -r--r--r-- */
256 pfs->pfs_mode = S_IRUSR|S_IRGRP|S_IROTH;
257 vp->v_type = VREG;
258 break;
259
260 #ifdef __HAVE_PROCFS_MACHDEP
261 PROCFS_MACHDEP_NODETYPE_CASES
262 procfs_machdep_allocvp(vp);
263 break;
264 #endif
265
266 default:
267 panic("procfs_allocvp");
268 }
269
270 procfs_hashins(pfs);
271 uvm_vnp_setsize(vp, 0);
272 lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
273
274 *vpp = vp;
275 return (0);
276
277 bad:
278 lockmgr(&pfs_hashlock, LK_RELEASE, NULL);
279 FREE(pfs, M_TEMP);
280 ungetnewvnode(vp);
281 return (error);
282 }
283
284 int
285 procfs_freevp(vp)
286 struct vnode *vp;
287 {
288 struct pfsnode *pfs = VTOPFS(vp);
289
290 procfs_hashrem(pfs);
291
292 FREE(vp->v_data, M_TEMP);
293 vp->v_data = 0;
294 return (0);
295 }
296
297 int
298 procfs_rw(v)
299 void *v;
300 {
301 struct vop_read_args *ap = v;
302 struct vnode *vp = ap->a_vp;
303 struct uio *uio = ap->a_uio;
304 struct lwp *curl;
305 struct lwp *l;
306 struct pfsnode *pfs = VTOPFS(vp);
307 struct proc *p;
308 int error;
309
310 if (uio->uio_offset < 0)
311 return EINVAL;
312 p = PFIND(pfs->pfs_pid);
313 if (p == 0)
314 return ESRCH;
315
316 if (ISSET(p->p_flag, P_INEXEC))
317 return (EAGAIN);
318
319 curl = curlwp;
320
321 /*
322 * Do not allow init to be modified while in secure mode; it
323 * could be duped into changing the security level.
324 */
325 #define M2K(m) ((m) == UIO_READ ? KAUTH_REQ_PROCESS_CANPROCFS_READ : \
326 KAUTH_REQ_PROCESS_CANPROCFS_WRITE)
327 error = kauth_authorize_process(curl->l_cred, KAUTH_PROCESS_CANPROCFS,
328 p, pfs, KAUTH_ARG(M2K(uio->uio_rw)), NULL);
329 if (error)
330 return (error);
331 #undef M2K
332
333 /* XXX NJWLWP
334 * The entire procfs interface needs work to be useful to
335 * a process with multiple LWPs. For the moment, we'll
336 * just kluge this and fail on others.
337 */
338 l = proc_representative_lwp(p);
339
340 switch (pfs->pfs_type) {
341 case PFSnote:
342 case PFSnotepg:
343 return (procfs_donote(curl, p, pfs, uio));
344
345 case PFSregs:
346 return (procfs_doregs(curl, l, pfs, uio));
347
348 case PFSfpregs:
349 return (procfs_dofpregs(curl, l, pfs, uio));
350
351 case PFSctl:
352 return (procfs_doctl(curl, l, pfs, uio));
353
354 case PFSstatus:
355 return (procfs_dostatus(curl, l, pfs, uio));
356
357 case PFSstat:
358 return (procfs_do_pid_stat(curl, l, pfs, uio));
359
360 case PFSmap:
361 return (procfs_domap(curl, p, pfs, uio, 0));
362
363 case PFSmaps:
364 return (procfs_domap(curl, p, pfs, uio, 1));
365
366 case PFSmem:
367 return (procfs_domem(curl, l, pfs, uio));
368
369 case PFScmdline:
370 return (procfs_docmdline(curl, p, pfs, uio));
371
372 case PFSmeminfo:
373 return (procfs_domeminfo(curl, p, pfs, uio));
374
375 case PFSdevices:
376 return (procfs_dodevices(curl, p, pfs, uio));
377
378 case PFScpuinfo:
379 return (procfs_docpuinfo(curl, p, pfs, uio));
380
381 case PFSfd:
382 return (procfs_dofd(curl, p, pfs, uio));
383
384 case PFSuptime:
385 return (procfs_douptime(curl, p, pfs, uio));
386
387 case PFSmounts:
388 return (procfs_domounts(curl, p, pfs, uio));
389
390 case PFSemul:
391 return procfs_doemul(curl, p, pfs, uio);
392
393 #ifdef __HAVE_PROCFS_MACHDEP
394 PROCFS_MACHDEP_NODETYPE_CASES
395 return (procfs_machdep_rw(curl, l, pfs, uio));
396 #endif
397
398 default:
399 return (EOPNOTSUPP);
400 }
401 }
402
403 /*
404 * Get a string from userland into (bf). Strip a trailing
405 * nl character (to allow easy access from the shell).
406 * The buffer should be *buflenp + 1 chars long. vfs_getuserstr
407 * will automatically add a nul char at the end.
408 *
409 * Returns 0 on success or the following errors
410 *
411 * EINVAL: file offset is non-zero.
412 * EMSGSIZE: message is longer than kernel buffer
413 * EFAULT: user i/o buffer is not addressable
414 */
415 int
416 vfs_getuserstr(uio, bf, buflenp)
417 struct uio *uio;
418 char *bf;
419 int *buflenp;
420 {
421 int xlen;
422 int error;
423
424 if (uio->uio_offset != 0)
425 return (EINVAL);
426
427 xlen = *buflenp;
428
429 /* must be able to read the whole string in one go */
430 if (xlen < uio->uio_resid)
431 return (EMSGSIZE);
432 xlen = uio->uio_resid;
433
434 if ((error = uiomove(bf, xlen, uio)) != 0)
435 return (error);
436
437 /* allow multiple writes without seeks */
438 uio->uio_offset = 0;
439
440 /* cleanup string and remove trailing newline */
441 bf[xlen] = '\0';
442 xlen = strlen(bf);
443 if (xlen > 0 && bf[xlen-1] == '\n')
444 bf[--xlen] = '\0';
445 *buflenp = xlen;
446
447 return (0);
448 }
449
450 const vfs_namemap_t *
451 vfs_findname(nm, bf, buflen)
452 const vfs_namemap_t *nm;
453 const char *bf;
454 int buflen;
455 {
456
457 for (; nm->nm_name; nm++)
458 if (memcmp(bf, nm->nm_name, buflen+1) == 0)
459 return (nm);
460
461 return (0);
462 }
463
464 /*
465 * Initialize pfsnode hash table.
466 */
467 void
468 procfs_hashinit()
469 {
470 lockinit(&pfs_hashlock, PINOD, "pfs_hashlock", 0, 0);
471 pfs_hashtbl = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT,
472 M_WAITOK, &pfs_ihash);
473 simple_lock_init(&pfs_hash_slock);
474 }
475
476 void
477 procfs_hashreinit()
478 {
479 struct pfsnode *pp;
480 struct pfs_hashhead *oldhash, *hash;
481 u_long i, oldmask, mask, val;
482
483 hash = hashinit(desiredvnodes / 4, HASH_LIST, M_UFSMNT, M_WAITOK,
484 &mask);
485
486 simple_lock(&pfs_hash_slock);
487 oldhash = pfs_hashtbl;
488 oldmask = pfs_ihash;
489 pfs_hashtbl = hash;
490 pfs_ihash = mask;
491 for (i = 0; i <= oldmask; i++) {
492 while ((pp = LIST_FIRST(&oldhash[i])) != NULL) {
493 LIST_REMOVE(pp, pfs_hash);
494 val = PFSPIDHASH(pp->pfs_pid);
495 LIST_INSERT_HEAD(&hash[val], pp, pfs_hash);
496 }
497 }
498 simple_unlock(&pfs_hash_slock);
499 hashdone(oldhash, M_UFSMNT);
500 }
501
502 /*
503 * Free pfsnode hash table.
504 */
505 void
506 procfs_hashdone()
507 {
508 hashdone(pfs_hashtbl, M_UFSMNT);
509 }
510
511 struct vnode *
512 procfs_hashget(pid, type, fd, mp)
513 pid_t pid;
514 pfstype type;
515 int fd;
516 struct mount *mp;
517 {
518 struct pfs_hashhead *ppp;
519 struct pfsnode *pp;
520 struct vnode *vp;
521
522 loop:
523 simple_lock(&pfs_hash_slock);
524 ppp = &pfs_hashtbl[PFSPIDHASH(pid)];
525 LIST_FOREACH(pp, ppp, pfs_hash) {
526 vp = PFSTOV(pp);
527 if (pid == pp->pfs_pid && pp->pfs_type == type &&
528 pp->pfs_fd == fd && vp->v_mount == mp) {
529 simple_lock(&vp->v_interlock);
530 simple_unlock(&pfs_hash_slock);
531 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK))
532 goto loop;
533 return (vp);
534 }
535 }
536 simple_unlock(&pfs_hash_slock);
537 return (NULL);
538 }
539
540 /*
541 * Insert the pfsnode into the hash table and lock it.
542 */
543 void
544 procfs_hashins(pp)
545 struct pfsnode *pp;
546 {
547 struct pfs_hashhead *ppp;
548
549 /* lock the pfsnode, then put it on the appropriate hash list */
550 lockmgr(&pp->pfs_vnode->v_lock, LK_EXCLUSIVE, (struct simplelock *)0);
551
552 simple_lock(&pfs_hash_slock);
553 ppp = &pfs_hashtbl[PFSPIDHASH(pp->pfs_pid)];
554 LIST_INSERT_HEAD(ppp, pp, pfs_hash);
555 simple_unlock(&pfs_hash_slock);
556 }
557
558 /*
559 * Remove the pfsnode from the hash table.
560 */
561 void
562 procfs_hashrem(pp)
563 struct pfsnode *pp;
564 {
565 simple_lock(&pfs_hash_slock);
566 LIST_REMOVE(pp, pfs_hash);
567 simple_unlock(&pfs_hash_slock);
568 }
569
570 void
571 procfs_revoke_vnodes(p, arg)
572 struct proc *p;
573 void *arg;
574 {
575 struct pfsnode *pfs, *pnext;
576 struct vnode *vp;
577 struct mount *mp = (struct mount *)arg;
578 struct pfs_hashhead *ppp;
579
580 if (!(p->p_flag & P_SUGID))
581 return;
582
583 ppp = &pfs_hashtbl[PFSPIDHASH(p->p_pid)];
584 for (pfs = LIST_FIRST(ppp); pfs; pfs = pnext) {
585 vp = PFSTOV(pfs);
586 pnext = LIST_NEXT(pfs, pfs_hash);
587 if (vp->v_usecount > 0 && pfs->pfs_pid == p->p_pid &&
588 vp->v_mount == mp)
589 VOP_REVOKE(vp, REVOKEALL);
590 }
591 }
592
593 int
594 procfs_getfp(pfs, pown, fp)
595 struct pfsnode *pfs;
596 struct proc **pown;
597 struct file **fp;
598 {
599 struct proc *p = PFIND(pfs->pfs_pid);
600
601 if (p == NULL)
602 return ESRCH;
603
604 if (pfs->pfs_fd == -1)
605 return EINVAL;
606
607 if ((*fp = fd_getfile(p->p_fd, pfs->pfs_fd)) == NULL)
608 return EBADF;
609
610 *pown = p;
611 return 0;
612 }
613
614 int
615 procfs_doemul(struct lwp *curl, struct proc *p,
616 struct pfsnode *pfs, struct uio *uio)
617 {
618 const char *ename = p->p_emul->e_name;
619 return uiomove_frombuf(__UNCONST(ename), strlen(ename), uio);
620 }
621