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