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