procfs_vnops.c revision 1.65 1 /* $NetBSD: procfs_vnops.c,v 1.65 1999/08/03 20:19:20 wrstuden Exp $ */
2
3 /*
4 * Copyright (c) 1993 Jan-Simon Pendry
5 * Copyright (c) 1993, 1995
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
40 */
41
42 /*
43 * procfs vnode interface
44 */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/time.h>
49 #include <sys/kernel.h>
50 #include <sys/file.h>
51 #include <sys/proc.h>
52 #include <sys/vnode.h>
53 #include <sys/namei.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <sys/resourcevar.h>
57 #include <sys/ptrace.h>
58 #include <sys/stat.h>
59
60 #include <vm/vm.h> /* for PAGE_SIZE */
61
62 #include <machine/reg.h>
63
64 #include <miscfs/genfs/genfs.h>
65 #include <miscfs/procfs/procfs.h>
66
67 /*
68 * Vnode Operations.
69 *
70 */
71
72 /*
73 * This is a list of the valid names in the
74 * process-specific sub-directories. It is
75 * used in procfs_lookup and procfs_readdir
76 */
77 struct proc_target {
78 u_char pt_type;
79 u_char pt_namlen;
80 char *pt_name;
81 pfstype pt_pfstype;
82 int (*pt_valid) __P((struct proc *p));
83 } proc_targets[] = {
84 #define N(s) sizeof(s)-1, s
85 /* name type validp */
86 { DT_DIR, N("."), Pproc, NULL },
87 { DT_DIR, N(".."), Proot, NULL },
88 { DT_REG, N("file"), Pfile, procfs_validfile },
89 { DT_REG, N("mem"), Pmem, NULL },
90 { DT_REG, N("regs"), Pregs, procfs_validregs },
91 { DT_REG, N("fpregs"), Pfpregs, procfs_validfpregs },
92 { DT_REG, N("ctl"), Pctl, NULL },
93 { DT_REG, N("status"), Pstatus, NULL },
94 { DT_REG, N("note"), Pnote, NULL },
95 { DT_REG, N("notepg"), Pnotepg, NULL },
96 { DT_REG, N("map"), Pmap, procfs_validmap },
97 { DT_REG, N("cmdline"), Pcmdline, NULL },
98 #undef N
99 };
100 static int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
101
102 static pid_t atopid __P((const char *, u_int));
103
104 int procfs_lookup __P((void *));
105 #define procfs_create genfs_eopnotsupp_rele
106 #define procfs_mknod genfs_eopnotsupp_rele
107 int procfs_open __P((void *));
108 int procfs_close __P((void *));
109 int procfs_access __P((void *));
110 int procfs_getattr __P((void *));
111 int procfs_setattr __P((void *));
112 #define procfs_read procfs_rw
113 #define procfs_write procfs_rw
114 #define procfs_fcntl genfs_fcntl
115 #define procfs_ioctl genfs_enoioctl
116 #define procfs_poll genfs_poll
117 #define procfs_revoke genfs_revoke
118 #define procfs_mmap genfs_eopnotsupp
119 #define procfs_fsync genfs_nullop
120 #define procfs_seek genfs_nullop
121 #define procfs_remove genfs_eopnotsupp_rele
122 int procfs_link __P((void *));
123 #define procfs_rename genfs_eopnotsupp_rele
124 #define procfs_mkdir genfs_eopnotsupp_rele
125 #define procfs_rmdir genfs_eopnotsupp_rele
126 int procfs_symlink __P((void *));
127 int procfs_readdir __P((void *));
128 int procfs_readlink __P((void *));
129 #define procfs_abortop genfs_abortop
130 int procfs_inactive __P((void *));
131 int procfs_reclaim __P((void *));
132 #define procfs_lock genfs_lock
133 #define procfs_unlock genfs_unlock
134 int procfs_bmap __P((void *));
135 #define procfs_strategy genfs_badop
136 int procfs_print __P((void *));
137 int procfs_pathconf __P((void *));
138 #define procfs_islocked genfs_islocked
139 #define procfs_advlock genfs_einval
140 #define procfs_blkatoff genfs_eopnotsupp
141 #define procfs_valloc genfs_eopnotsupp
142 #define procfs_vfree genfs_nullop
143 #define procfs_truncate genfs_eopnotsupp
144 #define procfs_update genfs_nullop
145 #define procfs_bwrite genfs_eopnotsupp
146
147 static pid_t atopid __P((const char *, u_int));
148
149 /*
150 * procfs vnode operations.
151 */
152 int (**procfs_vnodeop_p) __P((void *));
153 struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
154 { &vop_default_desc, vn_default_error },
155 { &vop_lookup_desc, procfs_lookup }, /* lookup */
156 { &vop_create_desc, procfs_create }, /* create */
157 { &vop_mknod_desc, procfs_mknod }, /* mknod */
158 { &vop_open_desc, procfs_open }, /* open */
159 { &vop_close_desc, procfs_close }, /* close */
160 { &vop_access_desc, procfs_access }, /* access */
161 { &vop_getattr_desc, procfs_getattr }, /* getattr */
162 { &vop_setattr_desc, procfs_setattr }, /* setattr */
163 { &vop_read_desc, procfs_read }, /* read */
164 { &vop_write_desc, procfs_write }, /* write */
165 { &vop_fcntl_desc, procfs_fcntl }, /* fcntl */
166 { &vop_ioctl_desc, procfs_ioctl }, /* ioctl */
167 { &vop_poll_desc, procfs_poll }, /* poll */
168 { &vop_revoke_desc, procfs_revoke }, /* revoke */
169 { &vop_mmap_desc, procfs_mmap }, /* mmap */
170 { &vop_fsync_desc, procfs_fsync }, /* fsync */
171 { &vop_seek_desc, procfs_seek }, /* seek */
172 { &vop_remove_desc, procfs_remove }, /* remove */
173 { &vop_link_desc, procfs_link }, /* link */
174 { &vop_rename_desc, procfs_rename }, /* rename */
175 { &vop_mkdir_desc, procfs_mkdir }, /* mkdir */
176 { &vop_rmdir_desc, procfs_rmdir }, /* rmdir */
177 { &vop_symlink_desc, procfs_symlink }, /* symlink */
178 { &vop_readdir_desc, procfs_readdir }, /* readdir */
179 { &vop_readlink_desc, procfs_readlink }, /* readlink */
180 { &vop_abortop_desc, procfs_abortop }, /* abortop */
181 { &vop_inactive_desc, procfs_inactive }, /* inactive */
182 { &vop_reclaim_desc, procfs_reclaim }, /* reclaim */
183 { &vop_lock_desc, procfs_lock }, /* lock */
184 { &vop_unlock_desc, procfs_unlock }, /* unlock */
185 { &vop_bmap_desc, procfs_bmap }, /* bmap */
186 { &vop_strategy_desc, procfs_strategy }, /* strategy */
187 { &vop_print_desc, procfs_print }, /* print */
188 { &vop_islocked_desc, procfs_islocked }, /* islocked */
189 { &vop_pathconf_desc, procfs_pathconf }, /* pathconf */
190 { &vop_advlock_desc, procfs_advlock }, /* advlock */
191 { &vop_blkatoff_desc, procfs_blkatoff }, /* blkatoff */
192 { &vop_valloc_desc, procfs_valloc }, /* valloc */
193 { &vop_vfree_desc, procfs_vfree }, /* vfree */
194 { &vop_truncate_desc, procfs_truncate }, /* truncate */
195 { &vop_update_desc, procfs_update }, /* update */
196 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
197 };
198 struct vnodeopv_desc procfs_vnodeop_opv_desc =
199 { &procfs_vnodeop_p, procfs_vnodeop_entries };
200 /*
201 * set things up for doing i/o on
202 * the pfsnode (vp). (vp) is locked
203 * on entry, and should be left locked
204 * on exit.
205 *
206 * for procfs we don't need to do anything
207 * in particular for i/o. all that is done
208 * is to support exclusive open on process
209 * memory images.
210 */
211 int
212 procfs_open(v)
213 void *v;
214 {
215 struct vop_open_args /* {
216 struct vnode *a_vp;
217 int a_mode;
218 struct ucred *a_cred;
219 struct proc *a_p;
220 } */ *ap = v;
221 struct pfsnode *pfs = VTOPFS(ap->a_vp);
222 struct proc *p1, *p2;
223 int error;
224
225 p1 = ap->a_p; /* tracer */
226 p2 = PFIND(pfs->pfs_pid); /* traced */
227
228 if (p2 == NULL)
229 return (ENOENT); /* was ESRCH, jsp */
230
231 switch (pfs->pfs_type) {
232 case Pmem:
233 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
234 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
235 return (EBUSY);
236
237 if ((error = procfs_checkioperm(p1, p2)) != 0)
238 return (EPERM);
239
240 if (ap->a_mode & FWRITE)
241 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
242
243 return (0);
244
245 default:
246 break;
247 }
248
249 return (0);
250 }
251
252 /*
253 * close the pfsnode (vp) after doing i/o.
254 * (vp) is not locked on entry or exit.
255 *
256 * nothing to do for procfs other than undo
257 * any exclusive open flag (see _open above).
258 */
259 int
260 procfs_close(v)
261 void *v;
262 {
263 struct vop_close_args /* {
264 struct vnode *a_vp;
265 int a_fflag;
266 struct ucred *a_cred;
267 struct proc *a_p;
268 } */ *ap = v;
269 struct pfsnode *pfs = VTOPFS(ap->a_vp);
270
271 switch (pfs->pfs_type) {
272 case Pmem:
273 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
274 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
275 break;
276
277 default:
278 break;
279 }
280
281 return (0);
282 }
283
284 /*
285 * do block mapping for pfsnode (vp).
286 * since we don't use the buffer cache
287 * for procfs this function should never
288 * be called. in any case, it's not clear
289 * what part of the kernel ever makes use
290 * of this function. for sanity, this is the
291 * usual no-op bmap, although returning
292 * (EIO) would be a reasonable alternative.
293 */
294 int
295 procfs_bmap(v)
296 void *v;
297 {
298 struct vop_bmap_args /* {
299 struct vnode *a_vp;
300 daddr_t a_bn;
301 struct vnode **a_vpp;
302 daddr_t *a_bnp;
303 int * a_runp;
304 } */ *ap = v;
305
306 if (ap->a_vpp != NULL)
307 *ap->a_vpp = ap->a_vp;
308 if (ap->a_bnp != NULL)
309 *ap->a_bnp = ap->a_bn;
310 if (ap->a_runp != NULL)
311 *ap->a_runp = 0;
312 return (0);
313 }
314
315 /*
316 * _inactive is called when the pfsnode
317 * is vrele'd and the reference count goes
318 * to zero. (vp) will be on the vnode free
319 * list, so to get it back vget() must be
320 * used.
321 *
322 * for procfs, check if the process is still
323 * alive and if it isn't then just throw away
324 * the vnode by calling vgone(). this may
325 * be overkill and a waste of time since the
326 * chances are that the process will still be
327 * there and PFIND is not free.
328 *
329 * (vp) is locked on entry, but must be unlocked on exit.
330 */
331 int
332 procfs_inactive(v)
333 void *v;
334 {
335 struct vop_inactive_args /* {
336 struct vnode *a_vp;
337 struct proc *a_p;
338 } */ *ap = v;
339 struct pfsnode *pfs = VTOPFS(ap->a_vp);
340
341 VOP_UNLOCK(ap->a_vp, 0);
342 if (PFIND(pfs->pfs_pid) == 0)
343 vgone(ap->a_vp);
344
345 return (0);
346 }
347
348 /*
349 * _reclaim is called when getnewvnode()
350 * wants to make use of an entry on the vnode
351 * free list. at this time the filesystem needs
352 * to free any private data and remove the node
353 * from any private lists.
354 */
355 int
356 procfs_reclaim(v)
357 void *v;
358 {
359 struct vop_reclaim_args /* {
360 struct vnode *a_vp;
361 } */ *ap = v;
362
363 return (procfs_freevp(ap->a_vp));
364 }
365
366 /*
367 * Return POSIX pathconf information applicable to special devices.
368 */
369 int
370 procfs_pathconf(v)
371 void *v;
372 {
373 struct vop_pathconf_args /* {
374 struct vnode *a_vp;
375 int a_name;
376 register_t *a_retval;
377 } */ *ap = v;
378
379 switch (ap->a_name) {
380 case _PC_LINK_MAX:
381 *ap->a_retval = LINK_MAX;
382 return (0);
383 case _PC_MAX_CANON:
384 *ap->a_retval = MAX_CANON;
385 return (0);
386 case _PC_MAX_INPUT:
387 *ap->a_retval = MAX_INPUT;
388 return (0);
389 case _PC_PIPE_BUF:
390 *ap->a_retval = PIPE_BUF;
391 return (0);
392 case _PC_CHOWN_RESTRICTED:
393 *ap->a_retval = 1;
394 return (0);
395 case _PC_VDISABLE:
396 *ap->a_retval = _POSIX_VDISABLE;
397 return (0);
398 case _PC_SYNC_IO:
399 *ap->a_retval = 1;
400 return (0);
401 default:
402 return (EINVAL);
403 }
404 /* NOTREACHED */
405 }
406
407 /*
408 * _print is used for debugging.
409 * just print a readable description
410 * of (vp).
411 */
412 int
413 procfs_print(v)
414 void *v;
415 {
416 struct vop_print_args /* {
417 struct vnode *a_vp;
418 } */ *ap = v;
419 struct pfsnode *pfs = VTOPFS(ap->a_vp);
420
421 printf("tag VT_PROCFS, type %d, pid %d, mode %x, flags %lx\n",
422 pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
423 return 0;
424 }
425
426 int
427 procfs_link(v)
428 void *v;
429 {
430 struct vop_link_args /* {
431 struct vnode *a_dvp;
432 struct vnode *a_vp;
433 struct componentname *a_cnp;
434 } */ *ap = v;
435
436 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
437 vput(ap->a_dvp);
438 return (EROFS);
439 }
440
441 int
442 procfs_symlink(v)
443 void *v;
444 {
445 struct vop_symlink_args /* {
446 struct vnode *a_dvp;
447 struct vnode **a_vpp;
448 struct componentname *a_cnp;
449 struct vattr *a_vap;
450 char *a_target;
451 } */ *ap = v;
452
453 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
454 vput(ap->a_dvp);
455 return (EROFS);
456 }
457
458 /*
459 * Invent attributes for pfsnode (vp) and store
460 * them in (vap).
461 * Directories lengths are returned as zero since
462 * any real length would require the genuine size
463 * to be computed, and nothing cares anyway.
464 *
465 * this is relatively minimal for procfs.
466 */
467 int
468 procfs_getattr(v)
469 void *v;
470 {
471 struct vop_getattr_args /* {
472 struct vnode *a_vp;
473 struct vattr *a_vap;
474 struct ucred *a_cred;
475 struct proc *a_p;
476 } */ *ap = v;
477 struct pfsnode *pfs = VTOPFS(ap->a_vp);
478 struct vattr *vap = ap->a_vap;
479 struct proc *procp;
480 struct timeval tv;
481 int error;
482
483 /* first check the process still exists */
484 switch (pfs->pfs_type) {
485 case Proot:
486 case Pcurproc:
487 procp = 0;
488 break;
489
490 default:
491 procp = PFIND(pfs->pfs_pid);
492 if (procp == 0)
493 return (ENOENT);
494 break;
495 }
496
497 error = 0;
498
499 /* start by zeroing out the attributes */
500 VATTR_NULL(vap);
501
502 /* next do all the common fields */
503 vap->va_type = ap->a_vp->v_type;
504 vap->va_mode = pfs->pfs_mode;
505 vap->va_fileid = pfs->pfs_fileno;
506 vap->va_flags = 0;
507 vap->va_blocksize = PAGE_SIZE;
508
509 /*
510 * Make all times be current TOD.
511 * It would be possible to get the process start
512 * time from the p_stat structure, but there's
513 * no "file creation" time stamp anyway, and the
514 * p_stat structure is not addressible if u. gets
515 * swapped out for that process.
516 */
517 microtime(&tv);
518 TIMEVAL_TO_TIMESPEC(&tv, &vap->va_ctime);
519 vap->va_atime = vap->va_mtime = vap->va_ctime;
520
521 switch (pfs->pfs_type) {
522 case Pmem:
523 case Pregs:
524 case Pfpregs:
525 /*
526 * If the process has exercised some setuid or setgid
527 * privilege, then rip away read/write permission so
528 * that only root can gain access.
529 */
530 if (procp->p_flag & P_SUGID)
531 vap->va_mode &= ~(S_IRUSR|S_IWUSR);
532 /* FALLTHROUGH */
533 case Pctl:
534 case Pstatus:
535 case Pnote:
536 case Pnotepg:
537 case Pmap:
538 case Pcmdline:
539 vap->va_nlink = 1;
540 vap->va_uid = procp->p_ucred->cr_uid;
541 vap->va_gid = procp->p_ucred->cr_gid;
542 break;
543
544 default:
545 break;
546 }
547
548 /*
549 * now do the object specific fields
550 *
551 * The size could be set from struct reg, but it's hardly
552 * worth the trouble, and it puts some (potentially) machine
553 * dependent data into this machine-independent code. If it
554 * becomes important then this function should break out into
555 * a per-file stat function in the corresponding .c file.
556 */
557
558 switch (pfs->pfs_type) {
559 case Proot:
560 /*
561 * Set nlink to 1 to tell fts(3) we don't actually know.
562 */
563 vap->va_nlink = 1;
564 vap->va_uid = 0;
565 vap->va_gid = 0;
566 vap->va_bytes = vap->va_size = DEV_BSIZE;
567 break;
568
569 case Pcurproc: {
570 char buf[16]; /* should be enough */
571 vap->va_nlink = 1;
572 vap->va_uid = 0;
573 vap->va_gid = 0;
574 vap->va_bytes = vap->va_size =
575 sprintf(buf, "%ld", (long)curproc->p_pid);
576 break;
577 }
578
579 case Pproc:
580 vap->va_nlink = 2;
581 vap->va_uid = procp->p_ucred->cr_uid;
582 vap->va_gid = procp->p_ucred->cr_gid;
583 vap->va_bytes = vap->va_size = DEV_BSIZE;
584 break;
585
586 case Pfile:
587 error = EOPNOTSUPP;
588 break;
589
590 case Pmem:
591 vap->va_bytes = vap->va_size =
592 ctob(procp->p_vmspace->vm_tsize +
593 procp->p_vmspace->vm_dsize +
594 procp->p_vmspace->vm_ssize);
595 break;
596
597 #if defined(PT_GETREGS) || defined(PT_SETREGS)
598 case Pregs:
599 vap->va_bytes = vap->va_size = sizeof(struct reg);
600 break;
601 #endif
602
603 #if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
604 case Pfpregs:
605 vap->va_bytes = vap->va_size = sizeof(struct fpreg);
606 break;
607 #endif
608
609 case Pctl:
610 case Pstatus:
611 case Pnote:
612 case Pnotepg:
613 case Pmap:
614 case Pcmdline:
615 vap->va_bytes = vap->va_size = 0;
616 break;
617
618 default:
619 panic("procfs_getattr");
620 }
621
622 return (error);
623 }
624
625 /*ARGSUSED*/
626 int
627 procfs_setattr(v)
628 void *v;
629 {
630 /*
631 * just fake out attribute setting
632 * it's not good to generate an error
633 * return, otherwise things like creat()
634 * will fail when they try to set the
635 * file length to 0. worse, this means
636 * that echo $note > /proc/$pid/note will fail.
637 */
638
639 return (0);
640 }
641
642 /*
643 * implement access checking.
644 *
645 * actually, the check for super-user is slightly
646 * broken since it will allow read access to write-only
647 * objects. this doesn't cause any particular trouble
648 * but does mean that the i/o entry points need to check
649 * that the operation really does make sense.
650 */
651 int
652 procfs_access(v)
653 void *v;
654 {
655 struct vop_access_args /* {
656 struct vnode *a_vp;
657 int a_mode;
658 struct ucred *a_cred;
659 struct proc *a_p;
660 } */ *ap = v;
661 struct vattr va;
662 int error;
663
664 if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
665 return (error);
666
667 return (vaccess(va.va_type, va.va_mode,
668 va.va_uid, va.va_gid, ap->a_mode, ap->a_cred));
669 }
670
671 /*
672 * lookup. this is incredibly complicated in the
673 * general case, however for most pseudo-filesystems
674 * very little needs to be done.
675 *
676 * Locking isn't hard here, just poorly documented.
677 *
678 * If we're looking up ".", just vref the parent & return it.
679 *
680 * If we're looking up "..", unlock the parent, and lock "..". If everything
681 * went ok, and we're on the last component and the caller requested the
682 * parent locked, try to re-lock the parent. We do this to prevent lock
683 * races.
684 *
685 * For anything else, get the needed node. Then unlock the parent if not
686 * the last component or not LOCKPARENT (i.e. if we wouldn't re-lock the
687 * parent in the .. case).
688 *
689 * We try to exit with the parent locked in error cases.
690 */
691 int
692 procfs_lookup(v)
693 void *v;
694 {
695 struct vop_lookup_args /* {
696 struct vnode * a_dvp;
697 struct vnode ** a_vpp;
698 struct componentname * a_cnp;
699 } */ *ap = v;
700 struct componentname *cnp = ap->a_cnp;
701 struct vnode **vpp = ap->a_vpp;
702 struct vnode *dvp = ap->a_dvp;
703 const char *pname = cnp->cn_nameptr;
704 struct proc_target *pt;
705 struct vnode *fvp;
706 pid_t pid;
707 struct pfsnode *pfs;
708 struct proc *p;
709 int i, error, wantpunlock;
710
711 *vpp = NULL;
712 cnp->cn_flags &= ~PDIRUNLOCK;
713
714 if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
715 return (EROFS);
716
717 if (cnp->cn_namelen == 1 && *pname == '.') {
718 *vpp = dvp;
719 VREF(dvp);
720 return (0);
721 }
722
723 wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
724 pfs = VTOPFS(dvp);
725 switch (pfs->pfs_type) {
726 case Proot:
727 /*
728 * Shouldn't get here with .. in the root node.
729 */
730 if (cnp->cn_flags & ISDOTDOT)
731 return (EIO);
732
733 if (CNEQ(cnp, "curproc", 7)) {
734 error = procfs_allocvp(dvp->v_mount, vpp, 0, Pcurproc);
735 if ((error == 0) && (wantpunlock)) {
736 VOP_UNLOCK(dvp, 0);
737 cnp->cn_flags |= PDIRUNLOCK;
738 }
739 return (error);
740 }
741
742 pid = atopid(pname, cnp->cn_namelen);
743 if (pid == NO_PID)
744 break;
745
746 p = PFIND(pid);
747 if (p == 0)
748 break;
749
750 error = procfs_allocvp(dvp->v_mount, vpp, pid, Pproc);
751 if ((error == 0) && (wantpunlock)) {
752 VOP_UNLOCK(dvp, 0);
753 cnp->cn_flags |= PDIRUNLOCK;
754 }
755 return (error);
756
757 case Pproc:
758 /*
759 * do the .. dance. We unlock the directory, and then
760 * get the root dir. That will automatically return ..
761 * locked. Then if the caller wanted dvp locked, we
762 * re-lock.
763 */
764 if (cnp->cn_flags & ISDOTDOT) {
765 VOP_UNLOCK(dvp, 0);
766 cnp->cn_flags |= PDIRUNLOCK;
767 error = procfs_root(dvp->v_mount, vpp);
768 if ((error == 0) && (wantpunlock == 0) &&
769 ((error = vn_lock(dvp, LK_EXCLUSIVE)) == 0))
770 cnp->cn_flags &= ~PDIRUNLOCK;
771 return (error);
772 }
773
774 p = PFIND(pfs->pfs_pid);
775 if (p == 0)
776 break;
777
778 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
779 if (cnp->cn_namelen == pt->pt_namlen &&
780 memcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
781 (pt->pt_valid == NULL || (*pt->pt_valid)(p)))
782 goto found;
783 }
784 break;
785
786 found:
787 if (pt->pt_pfstype == Pfile) {
788 fvp = procfs_findtextvp(p);
789 /* We already checked that it exists. */
790 VREF(fvp);
791 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
792 if (wantpunlock) {
793 VOP_UNLOCK(dvp, 0);
794 cnp->cn_flags |= PDIRUNLOCK;
795 }
796 *vpp = fvp;
797 return (0);
798 }
799
800 error = procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
801 pt->pt_pfstype);
802 if ((error == 0) && (wantpunlock)) {
803 VOP_UNLOCK(dvp, 0);
804 cnp->cn_flags |= PDIRUNLOCK;
805 }
806 return (error);
807
808 default:
809 return (ENOTDIR);
810 }
811
812 return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
813 }
814
815 int
816 procfs_validfile(p)
817 struct proc *p;
818 {
819
820 return (procfs_findtextvp(p) != NULLVP);
821 }
822
823 /*
824 * readdir returns directory entries from pfsnode (vp).
825 *
826 * the strategy here with procfs is to generate a single
827 * directory entry at a time (struct dirent) and then
828 * copy that out to userland using uiomove. a more efficent
829 * though more complex implementation, would try to minimize
830 * the number of calls to uiomove(). for procfs, this is
831 * hardly worth the added code complexity.
832 *
833 * this should just be done through read()
834 */
835 int
836 procfs_readdir(v)
837 void *v;
838 {
839 struct vop_readdir_args /* {
840 struct vnode *a_vp;
841 struct uio *a_uio;
842 struct ucred *a_cred;
843 int *a_eofflag;
844 off_t **a_cookies;
845 int *a_ncookies;
846 } */ *ap = v;
847 struct uio *uio = ap->a_uio;
848 struct dirent d;
849 struct pfsnode *pfs;
850 int i;
851 int error;
852 off_t *cookies = NULL;
853 int ncookies;
854
855 pfs = VTOPFS(ap->a_vp);
856
857 if (uio->uio_resid < UIO_MX)
858 return (EINVAL);
859 if (uio->uio_offset < 0)
860 return (EINVAL);
861
862 error = 0;
863 i = uio->uio_offset;
864 memset((caddr_t)&d, 0, UIO_MX);
865 d.d_reclen = UIO_MX;
866 ncookies = uio->uio_resid / UIO_MX;
867
868 switch (pfs->pfs_type) {
869 /*
870 * this is for the process-specific sub-directories.
871 * all that is needed to is copy out all the entries
872 * from the procent[] table (top of this file).
873 */
874 case Pproc: {
875 struct proc *p;
876 struct proc_target *pt;
877
878 p = PFIND(pfs->pfs_pid);
879 if (p == NULL)
880 break;
881
882 if (ap->a_ncookies) {
883 ncookies = min(ncookies, (nproc_targets - i));
884 MALLOC(cookies, off_t *, ncookies * sizeof (off_t),
885 M_TEMP, M_WAITOK);
886 *ap->a_cookies = cookies;
887 }
888
889 for (pt = &proc_targets[i];
890 uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
891 if (pt->pt_valid && (*pt->pt_valid)(p) == 0)
892 continue;
893
894 d.d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype);
895 d.d_namlen = pt->pt_namlen;
896 memcpy(d.d_name, pt->pt_name, pt->pt_namlen + 1);
897 d.d_type = pt->pt_type;
898
899 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
900 break;
901 if (cookies)
902 *cookies++ = i + 1;
903 }
904
905 break;
906 }
907
908 /*
909 * this is for the root of the procfs filesystem
910 * what is needed is a special entry for "curproc"
911 * followed by an entry for each process on allproc
912 #ifdef PROCFS_ZOMBIE
913 * and deadproc and zombproc.
914 #endif
915 */
916
917 case Proot: {
918 int pcnt = i, nc = 0;
919 const struct proclist_desc *pd;
920 volatile struct proc *p;
921
922 if (pcnt > 3)
923 pcnt = 3;
924 if (ap->a_ncookies) {
925 /*
926 * XXX Potentially allocating too much space here,
927 * but I'm lazy. This loop needs some work.
928 */
929 MALLOC(cookies, off_t *, ncookies * sizeof (off_t),
930 M_TEMP, M_WAITOK);
931 *ap->a_cookies = cookies;
932 }
933 /*
934 * XXX: THIS LOOP ASSUMES THAT allproc IS THE FIRST
935 * PROCLIST IN THE proclists!
936 */
937 proclist_lock_read();
938 pd = proclists;
939 #ifdef PROCFS_ZOMBIE
940 again:
941 #endif
942 for (p = LIST_FIRST(pd->pd_list);
943 p != NULL && uio->uio_resid >= UIO_MX; i++, pcnt++) {
944 switch (i) {
945 case 0: /* `.' */
946 case 1: /* `..' */
947 d.d_fileno = PROCFS_FILENO(0, Proot);
948 d.d_namlen = i + 1;
949 memcpy(d.d_name, "..", d.d_namlen);
950 d.d_name[i + 1] = '\0';
951 d.d_type = DT_DIR;
952 break;
953
954 case 2:
955 d.d_fileno = PROCFS_FILENO(0, Pcurproc);
956 d.d_namlen = 7;
957 memcpy(d.d_name, "curproc", 8);
958 d.d_type = DT_LNK;
959 break;
960
961 default:
962 while (pcnt < i) {
963 pcnt++;
964 p = LIST_NEXT(p, p_list);
965 if (!p)
966 goto done;
967 }
968 d.d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
969 d.d_namlen = sprintf(d.d_name, "%ld",
970 (long)p->p_pid);
971 d.d_type = DT_REG;
972 p = p->p_list.le_next;
973 break;
974 }
975
976 if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
977 break;
978 nc++;
979 if (cookies)
980 *cookies++ = i + 1;
981 }
982 done:
983
984 #ifdef PROCFS_ZOMBIE
985 pd++;
986 if (p == NULL && pd->pd_list != NULL)
987 goto again;
988 #endif
989 proclist_unlock_read();
990 ncookies = nc;
991
992 break;
993
994 }
995
996 default:
997 error = ENOTDIR;
998 break;
999 }
1000
1001 if (ap->a_ncookies) {
1002 if (error) {
1003 if (cookies)
1004 FREE(*ap->a_cookies, M_TEMP);
1005 *ap->a_ncookies = 0;
1006 *ap->a_cookies = NULL;
1007 } else
1008 *ap->a_ncookies = ncookies;
1009 }
1010 uio->uio_offset = i;
1011 return (error);
1012 }
1013
1014 /*
1015 * readlink reads the link of `curproc'
1016 */
1017 int
1018 procfs_readlink(v)
1019 void *v;
1020 {
1021 struct vop_readlink_args *ap = v;
1022 char buf[16]; /* should be enough */
1023 int len;
1024
1025 if (VTOPFS(ap->a_vp)->pfs_fileno != PROCFS_FILENO(0, Pcurproc))
1026 return (EINVAL);
1027
1028 len = sprintf(buf, "%ld", (long)curproc->p_pid);
1029
1030 return (uiomove((caddr_t)buf, len, ap->a_uio));
1031 }
1032
1033 /*
1034 * convert decimal ascii to pid_t
1035 */
1036 static pid_t
1037 atopid(b, len)
1038 const char *b;
1039 u_int len;
1040 {
1041 pid_t p = 0;
1042
1043 while (len--) {
1044 char c = *b++;
1045 if (c < '0' || c > '9')
1046 return (NO_PID);
1047 p = 10 * p + (c - '0');
1048 if (p > PID_MAX)
1049 return (NO_PID);
1050 }
1051
1052 return (p);
1053 }
1054