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