d33efa3805615c63417d4897cb99f51b4dfa42d0
[dragonfly.git] / sys / vfs / procfs / procfs_vnops.c
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  * $FreeBSD: src/sys/miscfs/procfs/procfs_vnops.c,v 1.76.2.7 2002/01/22 17:22:59 nectar Exp $
40  * $DragonFly: src/sys/vfs/procfs/procfs_vnops.c,v 1.7 2003/08/07 21:17:43 dillon 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/lock.h>
52 #include <sys/fcntl.h>
53 #include <sys/proc.h>
54 #include <sys/signalvar.h>
55 #include <sys/vnode.h>
56 #include <sys/uio.h>
57 #include <sys/mount.h>
58 #include <sys/namei.h>
59 #include <sys/dirent.h>
60 #include <sys/malloc.h>
61 #include <machine/reg.h>
62 #include <vm/vm_zone.h>
63 #include <vfs/procfs/procfs.h>
64 #include <sys/pioctl.h>
65
66 static int      procfs_access __P((struct vop_access_args *));
67 static int      procfs_badop __P((void));
68 static int      procfs_bmap __P((struct vop_bmap_args *));
69 static int      procfs_close __P((struct vop_close_args *));
70 static int      procfs_getattr __P((struct vop_getattr_args *));
71 static int      procfs_inactive __P((struct vop_inactive_args *));
72 static int      procfs_ioctl __P((struct vop_ioctl_args *));
73 static int      procfs_lookup __P((struct vop_lookup_args *));
74 static int      procfs_open __P((struct vop_open_args *));
75 static int      procfs_print __P((struct vop_print_args *));
76 static int      procfs_readdir __P((struct vop_readdir_args *));
77 static int      procfs_readlink __P((struct vop_readlink_args *));
78 static int      procfs_reclaim __P((struct vop_reclaim_args *));
79 static int      procfs_setattr __P((struct vop_setattr_args *));
80
81 /*
82  * This is a list of the valid names in the
83  * process-specific sub-directories.  It is
84  * used in procfs_lookup and procfs_readdir
85  */
86 static struct proc_target {
87         u_char  pt_type;
88         u_char  pt_namlen;
89         char    *pt_name;
90         pfstype pt_pfstype;
91         int     (*pt_valid) __P((struct proc *p));
92 } proc_targets[] = {
93 #define N(s) sizeof(s)-1, s
94         /*        name          type            validp */
95         { DT_DIR, N("."),       Pproc,          NULL },
96         { DT_DIR, N(".."),      Proot,          NULL },
97         { DT_REG, N("mem"),     Pmem,           NULL },
98         { DT_REG, N("regs"),    Pregs,          procfs_validregs },
99         { DT_REG, N("fpregs"),  Pfpregs,        procfs_validfpregs },
100         { DT_REG, N("dbregs"),  Pdbregs,        procfs_validdbregs },
101         { DT_REG, N("ctl"),     Pctl,           NULL },
102         { DT_REG, N("status"),  Pstatus,        NULL },
103         { DT_REG, N("note"),    Pnote,          NULL },
104         { DT_REG, N("notepg"),  Pnotepg,        NULL },
105         { DT_REG, N("map"),     Pmap,           procfs_validmap },
106         { DT_REG, N("etype"),   Ptype,          procfs_validtype },
107         { DT_REG, N("cmdline"), Pcmdline,       NULL },
108         { DT_REG, N("rlimit"),  Prlimit,        NULL },
109         { DT_LNK, N("file"),    Pfile,          NULL },
110 #undef N
111 };
112 static const int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
113
114 static pid_t atopid __P((const char *, u_int));
115
116 /*
117  * set things up for doing i/o on
118  * the pfsnode (vp).  (vp) is locked
119  * on entry, and should be left locked
120  * on exit.
121  *
122  * for procfs we don't need to do anything
123  * in particular for i/o.  all that is done
124  * is to support exclusive open on process
125  * memory images.
126  */
127 static int
128 procfs_open(ap)
129         struct vop_open_args /* {
130                 struct vnode *a_vp;
131                 int  a_mode;
132                 struct ucred *a_cred;
133                 struct thread *a_td;
134         } */ *ap;
135 {
136         struct pfsnode *pfs = VTOPFS(ap->a_vp);
137         struct proc *p1, *p2;
138
139         p2 = PFIND(pfs->pfs_pid);
140         if (p2 == NULL)
141                 return (ENOENT);
142         if (pfs->pfs_pid && !PRISON_CHECK(ap->a_cred, p2->p_ucred))
143                 return (ENOENT);
144
145         switch (pfs->pfs_type) {
146         case Pmem:
147                 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
148                     ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
149                         return (EBUSY);
150
151                 p1 = ap->a_td->td_proc;
152                 KKASSERT(p1);
153                 /* Can't trace a process that's currently exec'ing. */ 
154                 if ((p2->p_flag & P_INEXEC) != 0)
155                         return EAGAIN;
156                 if (!CHECKIO(p1, p2) || p_trespass(ap->a_cred, p2->p_ucred))
157                         return (EPERM);
158
159                 if (ap->a_mode & FWRITE)
160                         pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
161
162                 return (0);
163
164         default:
165                 break;
166         }
167
168         return (0);
169 }
170
171 /*
172  * close the pfsnode (vp) after doing i/o.
173  * (vp) is not locked on entry or exit.
174  *
175  * nothing to do for procfs other than undo
176  * any exclusive open flag (see _open above).
177  */
178 static int
179 procfs_close(ap)
180         struct vop_close_args /* {
181                 struct vnode *a_vp;
182                 int  a_fflag;
183                 struct ucred *a_cred;
184                 struct thread *a_td;
185         } */ *ap;
186 {
187         struct pfsnode *pfs = VTOPFS(ap->a_vp);
188         struct proc *p;
189
190         switch (pfs->pfs_type) {
191         case Pmem:
192                 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
193                         pfs->pfs_flags &= ~(FWRITE|O_EXCL);
194                 /*
195                  * This rather complicated-looking code is trying to
196                  * determine if this was the last close on this particular
197                  * vnode.  While one would expect v_usecount to be 1 at
198                  * that point, it seems that (according to John Dyson)
199                  * the VM system will bump up the usecount.  So:  if the
200                  * usecount is 2, and VOBJBUF is set, then this is really
201                  * the last close.  Otherwise, if the usecount is < 2
202                  * then it is definitely the last close.
203                  * If this is the last close, then it checks to see if
204                  * the target process has PF_LINGER set in p_pfsflags,
205                  * if this is *not* the case, then the process' stop flags
206                  * are cleared, and the process is woken up.  This is
207                  * to help prevent the case where a process has been
208                  * told to stop on an event, but then the requesting process
209                  * has gone away or forgotten about it.
210                  */
211                 if ((ap->a_vp->v_usecount < 2)
212                     && (p = pfind(pfs->pfs_pid))
213                     && !(p->p_pfsflags & PF_LINGER)) {
214                         p->p_stops = 0;
215                         p->p_step = 0;
216                         wakeup(&p->p_step);
217                 }
218                 break;
219         default:
220                 break;
221         }
222
223         return (0);
224 }
225
226 /*
227  * do an ioctl operation on a pfsnode (vp).
228  * (vp) is not locked on entry or exit.
229  */
230 static int
231 procfs_ioctl(ap)
232         struct vop_ioctl_args *ap;
233 {
234         struct pfsnode *pfs = VTOPFS(ap->a_vp);
235         struct proc *procp;
236         struct proc *p;
237         int error;
238         int signo;
239         struct procfs_status *psp;
240         unsigned char flags;
241
242         procp = pfind(pfs->pfs_pid);
243         if (procp == NULL)
244                 return ENOTTY;
245         p = ap->a_td->td_proc;
246         if (p == NULL)
247                 return EINVAL;
248
249         /* Can't trace a process that's currently exec'ing. */ 
250         if ((procp->p_flag & P_INEXEC) != 0)
251                 return EAGAIN;
252         if (!CHECKIO(p, procp) || p_trespass(ap->a_cred, procp->p_ucred))
253                 return EPERM;
254
255         switch (ap->a_command) {
256         case PIOCBIS:
257           procp->p_stops |= *(unsigned int*)ap->a_data;
258           break;
259         case PIOCBIC:
260           procp->p_stops &= ~*(unsigned int*)ap->a_data;
261           break;
262         case PIOCSFL:
263           /*
264            * NFLAGS is "non-suser_xxx flags" -- currently, only
265            * PFS_ISUGID ("ignore set u/g id");
266            */
267 #define NFLAGS  (PF_ISUGID)
268           flags = (unsigned char)*(unsigned int*)ap->a_data;
269           if (flags & NFLAGS && (error = suser_cred(ap->a_cred, 0)))
270             return error;
271           procp->p_pfsflags = flags;
272           break;
273         case PIOCGFL:
274           *(unsigned int*)ap->a_data = (unsigned int)procp->p_pfsflags;
275           break;
276         case PIOCSTATUS:
277           psp = (struct procfs_status *)ap->a_data;
278           psp->state = (procp->p_step == 0);
279           psp->flags = procp->p_pfsflags;
280           psp->events = procp->p_stops;
281           if (procp->p_step) {
282             psp->why = procp->p_stype;
283             psp->val = procp->p_xstat;
284           } else {
285             psp->why = psp->val = 0;    /* Not defined values */
286           }
287           break;
288         case PIOCWAIT:
289           psp = (struct procfs_status *)ap->a_data;
290           if (procp->p_step == 0) {
291             error = tsleep(&procp->p_stype, PCATCH, "piocwait", 0);
292             if (error)
293               return error;
294           }
295           psp->state = 1;       /* It stopped */
296           psp->flags = procp->p_pfsflags;
297           psp->events = procp->p_stops;
298           psp->why = procp->p_stype;    /* why it stopped */
299           psp->val = procp->p_xstat;    /* any extra info */
300           break;
301         case PIOCCONT:  /* Restart a proc */
302           if (procp->p_step == 0)
303             return EINVAL;      /* Can only start a stopped process */
304           if ((signo = *(int*)ap->a_data) != 0) {
305             if (signo >= NSIG || signo <= 0)
306               return EINVAL;
307             psignal(procp, signo);
308           }
309           procp->p_step = 0;
310           wakeup(&procp->p_step);
311           break;
312         default:
313           return (ENOTTY);
314         }
315         return 0;
316 }
317
318 /*
319  * do block mapping for pfsnode (vp).
320  * since we don't use the buffer cache
321  * for procfs this function should never
322  * be called.  in any case, it's not clear
323  * what part of the kernel ever makes use
324  * of this function.  for sanity, this is the
325  * usual no-op bmap, although returning
326  * (EIO) would be a reasonable alternative.
327  */
328 static int
329 procfs_bmap(ap)
330         struct vop_bmap_args /* {
331                 struct vnode *a_vp;
332                 daddr_t  a_bn;
333                 struct vnode **a_vpp;
334                 daddr_t *a_bnp;
335                 int *a_runp;
336         } */ *ap;
337 {
338
339         if (ap->a_vpp != NULL)
340                 *ap->a_vpp = ap->a_vp;
341         if (ap->a_bnp != NULL)
342                 *ap->a_bnp = ap->a_bn;
343         if (ap->a_runp != NULL)
344                 *ap->a_runp = 0;
345         return (0);
346 }
347
348 /*
349  * procfs_inactive is called when the pfsnode
350  * is vrele'd and the reference count goes
351  * to zero.  (vp) will be on the vnode free
352  * list, so to get it back vget() must be
353  * used.
354  *
355  * (vp) is locked on entry, but must be unlocked on exit.
356  */
357 static int
358 procfs_inactive(ap)
359         struct vop_inactive_args /* {
360                 struct vnode *a_vp;
361                 struct thread *a_td;
362         } */ *ap;
363 {
364         struct vnode *vp = ap->a_vp;
365
366         VOP_UNLOCK(vp, 0, ap->a_td);
367
368         return (0);
369 }
370
371 /*
372  * _reclaim is called when getnewvnode()
373  * wants to make use of an entry on the vnode
374  * free list.  at this time the filesystem needs
375  * to free any private data and remove the node
376  * from any private lists.
377  */
378 static int
379 procfs_reclaim(ap)
380         struct vop_reclaim_args /* {
381                 struct vnode *a_vp;
382         } */ *ap;
383 {
384
385         return (procfs_freevp(ap->a_vp));
386 }
387
388 /*
389  * _print is used for debugging.
390  * just print a readable description
391  * of (vp).
392  */
393 static int
394 procfs_print(ap)
395         struct vop_print_args /* {
396                 struct vnode *a_vp;
397         } */ *ap;
398 {
399         struct pfsnode *pfs = VTOPFS(ap->a_vp);
400
401         printf("tag VT_PROCFS, type %d, pid %ld, mode %x, flags %lx\n",
402             pfs->pfs_type, (long)pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
403         return (0);
404 }
405
406 /*
407  * generic entry point for unsupported operations
408  */
409 static int
410 procfs_badop()
411 {
412
413         return (EIO);
414 }
415
416 /*
417  * Invent attributes for pfsnode (vp) and store
418  * them in (vap).
419  * Directories lengths are returned as zero since
420  * any real length would require the genuine size
421  * to be computed, and nothing cares anyway.
422  *
423  * this is relatively minimal for procfs.
424  */
425 static int
426 procfs_getattr(ap)
427         struct vop_getattr_args /* {
428                 struct vnode *a_vp;
429                 struct vattr *a_vap;
430                 struct ucred *a_cred;
431                 struct thread *a_td;
432         } */ *ap;
433 {
434         struct pfsnode *pfs = VTOPFS(ap->a_vp);
435         struct vattr *vap = ap->a_vap;
436         struct proc *procp;
437         int error;
438
439         /*
440          * First make sure that the process and its credentials 
441          * still exist.
442          */
443         switch (pfs->pfs_type) {
444         case Proot:
445         case Pcurproc:
446                 procp = 0;
447                 break;
448
449         default:
450                 procp = PFIND(pfs->pfs_pid);
451                 if (procp == NULL || procp->p_ucred == NULL)
452                         return (ENOENT);
453         }
454
455         error = 0;
456
457         /* start by zeroing out the attributes */
458         VATTR_NULL(vap);
459
460         /* next do all the common fields */
461         vap->va_type = ap->a_vp->v_type;
462         vap->va_mode = pfs->pfs_mode;
463         vap->va_fileid = pfs->pfs_fileno;
464         vap->va_flags = 0;
465         vap->va_blocksize = PAGE_SIZE;
466         vap->va_bytes = vap->va_size = 0;
467         vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
468
469         /*
470          * Make all times be current TOD.
471          * It would be possible to get the process start
472          * time from the p_stat structure, but there's
473          * no "file creation" time stamp anyway, and the
474          * p_stat structure is not addressible if u. gets
475          * swapped out for that process.
476          */
477         nanotime(&vap->va_ctime);
478         vap->va_atime = vap->va_mtime = vap->va_ctime;
479
480         /*
481          * If the process has exercised some setuid or setgid
482          * privilege, then rip away read/write permission so
483          * that only root can gain access.
484          */
485         switch (pfs->pfs_type) {
486         case Pctl:
487         case Pregs:
488         case Pfpregs:
489         case Pdbregs:
490         case Pmem:
491                 if (procp->p_flag & P_SUGID)
492                         vap->va_mode &= ~((VREAD|VWRITE)|
493                                           ((VREAD|VWRITE)>>3)|
494                                           ((VREAD|VWRITE)>>6));
495                 break;
496         default:
497                 break;
498         }
499
500         /*
501          * now do the object specific fields
502          *
503          * The size could be set from struct reg, but it's hardly
504          * worth the trouble, and it puts some (potentially) machine
505          * dependent data into this machine-independent code.  If it
506          * becomes important then this function should break out into
507          * a per-file stat function in the corresponding .c file.
508          */
509
510         vap->va_nlink = 1;
511         if (procp) {
512                 vap->va_uid = procp->p_ucred->cr_uid;
513                 vap->va_gid = procp->p_ucred->cr_gid;
514         }
515
516         switch (pfs->pfs_type) {
517         case Proot:
518                 /*
519                  * Set nlink to 1 to tell fts(3) we don't actually know.
520                  */
521                 vap->va_nlink = 1;
522                 vap->va_uid = 0;
523                 vap->va_gid = 0;
524                 vap->va_size = vap->va_bytes = DEV_BSIZE;
525                 break;
526
527         case Pcurproc: {
528                 char buf[16];           /* should be enough */
529                 vap->va_uid = 0;
530                 vap->va_gid = 0;
531                 vap->va_size = vap->va_bytes =
532                     snprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
533                 break;
534         }
535
536         case Pproc:
537                 vap->va_nlink = nproc_targets;
538                 vap->va_size = vap->va_bytes = DEV_BSIZE;
539                 break;
540
541         case Pfile: {
542                 char *fullpath, *freepath;
543                 error = textvp_fullpath(procp, &fullpath, &freepath);
544                 if (error == 0) {
545                         vap->va_size = strlen(fullpath);
546                         free(freepath, M_TEMP);
547                 } else {
548                         vap->va_size = sizeof("unknown") - 1;
549                         error = 0;
550                 }
551                 vap->va_bytes = vap->va_size;
552                 break;
553         }
554
555         case Pmem:
556                 /*
557                  * If we denied owner access earlier, then we have to
558                  * change the owner to root - otherwise 'ps' and friends
559                  * will break even though they are setgid kmem. *SIGH*
560                  */
561                 if (procp->p_flag & P_SUGID)
562                         vap->va_uid = 0;
563                 else
564                         vap->va_uid = procp->p_ucred->cr_uid;
565                 break;
566
567         case Pregs:
568                 vap->va_bytes = vap->va_size = sizeof(struct reg);
569                 break;
570
571         case Pfpregs:
572                 vap->va_bytes = vap->va_size = sizeof(struct fpreg);
573                 break;
574
575         case Pdbregs:
576                 vap->va_bytes = vap->va_size = sizeof(struct dbreg);
577                 break;
578
579         case Ptype:
580         case Pmap:
581         case Pctl:
582         case Pstatus:
583         case Pnote:
584         case Pnotepg:
585         case Pcmdline:
586         case Prlimit:
587                 break;
588
589         default:
590                 panic("procfs_getattr");
591         }
592
593         return (error);
594 }
595
596 static int
597 procfs_setattr(ap)
598         struct vop_setattr_args /* {
599                 struct vnode *a_vp;
600                 struct vattr *a_vap;
601                 struct ucred *a_cred;
602                 struct thread *a_td;
603         } */ *ap;
604 {
605
606         if (ap->a_vap->va_flags != VNOVAL)
607                 return (EOPNOTSUPP);
608
609         /*
610          * just fake out attribute setting
611          * it's not good to generate an error
612          * return, otherwise things like creat()
613          * will fail when they try to set the
614          * file length to 0.  worse, this means
615          * that echo $note > /proc/$pid/note will fail.
616          */
617
618         return (0);
619 }
620
621 /*
622  * implement access checking.
623  *
624  * something very similar to this code is duplicated
625  * throughout the 4bsd kernel and should be moved
626  * into kern/vfs_subr.c sometime.
627  *
628  * actually, the check for super-user is slightly
629  * broken since it will allow read access to write-only
630  * objects.  this doesn't cause any particular trouble
631  * but does mean that the i/o entry points need to check
632  * that the operation really does make sense.
633  */
634 static int
635 procfs_access(ap)
636         struct vop_access_args /* {
637                 struct vnode *a_vp;
638                 int a_mode;
639                 struct ucred *a_cred;
640                 struct thread *a_td;
641         } */ *ap;
642 {
643         struct vattr *vap;
644         struct vattr vattr;
645         int error;
646
647         /*
648          * If you're the super-user,
649          * you always get access.
650          */
651         if (ap->a_cred->cr_uid == 0)
652                 return (0);
653
654         vap = &vattr;
655         error = VOP_GETATTR(ap->a_vp, vap, ap->a_td);
656         if (error)
657                 return (error);
658
659         /*
660          * Access check is based on only one of owner, group, public.
661          * If not owner, then check group. If not a member of the
662          * group, then check public access.
663          */
664         if (ap->a_cred->cr_uid != vap->va_uid) {
665                 gid_t *gp;
666                 int i;
667
668                 ap->a_mode >>= 3;
669                 gp = ap->a_cred->cr_groups;
670                 for (i = 0; i < ap->a_cred->cr_ngroups; i++, gp++)
671                         if (vap->va_gid == *gp)
672                                 goto found;
673                 ap->a_mode >>= 3;
674 found:
675                 ;
676         }
677
678         if ((vap->va_mode & ap->a_mode) == ap->a_mode)
679                 return (0);
680
681         return (EACCES);
682 }
683
684 /*
685  * lookup.  this is incredibly complicated in the
686  * general case, however for most pseudo-filesystems
687  * very little needs to be done.
688  *
689  * unless you want to get a migraine, just make sure your
690  * filesystem doesn't do any locking of its own.  otherwise
691  * read and inwardly digest ufs_lookup().
692  */
693 static int
694 procfs_lookup(ap)
695         struct vop_lookup_args /* {
696                 struct vnode * a_dvp;
697                 struct vnode ** a_vpp;
698                 struct componentname * a_cnp;
699         } */ *ap;
700 {
701         struct componentname *cnp = ap->a_cnp;
702         struct vnode **vpp = ap->a_vpp;
703         struct vnode *dvp = ap->a_dvp;
704         char *pname = cnp->cn_nameptr;
705         /* struct proc *curp = cnp->cn_proc; */
706         struct proc_target *pt;
707         pid_t pid;
708         struct pfsnode *pfs;
709         struct proc *p;
710         int i;
711
712         *vpp = NULL;
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                 /* vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, curp); */
721                 return (0);
722         }
723
724         pfs = VTOPFS(dvp);
725         switch (pfs->pfs_type) {
726         case Proot:
727                 if (cnp->cn_flags & ISDOTDOT)
728                         return (EIO);
729
730                 if (CNEQ(cnp, "curproc", 7))
731                         return (procfs_allocvp(dvp->v_mount, vpp, 0, Pcurproc));
732
733                 pid = atopid(pname, cnp->cn_namelen);
734                 if (pid == NO_PID)
735                         break;
736
737                 p = PFIND(pid);
738                 if (p == NULL)
739                         break;
740
741                 return (procfs_allocvp(dvp->v_mount, vpp, pid, Pproc));
742
743         case Pproc:
744                 if (cnp->cn_flags & ISDOTDOT)
745                         return (procfs_root(dvp->v_mount, vpp));
746
747                 p = PFIND(pfs->pfs_pid);
748                 if (p == NULL)
749                         break;
750
751                 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
752                         if (cnp->cn_namelen == pt->pt_namlen &&
753                             bcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
754                             (pt->pt_valid == NULL || (*pt->pt_valid)(p)))
755                                 goto found;
756                 }
757                 break;
758         found:
759                 return (procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
760                     pt->pt_pfstype));
761
762         default:
763                 return (ENOTDIR);
764         }
765
766         return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
767 }
768
769 /*
770  * Does this process have a text file?
771  */
772 int
773 procfs_validfile(p)
774         struct proc *p;
775 {
776
777         return (procfs_findtextvp(p) != NULLVP);
778 }
779
780 /*
781  * readdir() returns directory entries from pfsnode (vp).
782  *
783  * We generate just one directory entry at a time, as it would probably
784  * not pay off to buffer several entries locally to save uiomove calls.
785  */
786 static int
787 procfs_readdir(ap)
788         struct vop_readdir_args /* {
789                 struct vnode *a_vp;
790                 struct uio *a_uio;
791                 struct ucred *a_cred;
792                 int *a_eofflag;
793                 int *a_ncookies;
794                 u_long **a_cookies;
795         } */ *ap;
796 {
797         struct uio *uio = ap->a_uio;
798         struct dirent d;
799         struct dirent *dp = &d;
800         struct pfsnode *pfs;
801         int count, error, i, off;
802         static u_int delen;
803
804         if (!delen) {
805
806                 d.d_namlen = PROCFS_NAMELEN;
807                 delen = GENERIC_DIRSIZ(&d);
808         }
809
810         pfs = VTOPFS(ap->a_vp);
811
812         off = (int)uio->uio_offset;
813         if (off != uio->uio_offset || off < 0 || 
814             off % delen != 0 || uio->uio_resid < delen)
815                 return (EINVAL);
816
817         error = 0;
818         count = 0;
819         i = off / delen;
820
821         switch (pfs->pfs_type) {
822         /*
823          * this is for the process-specific sub-directories.
824          * all that is needed to is copy out all the entries
825          * from the procent[] table (top of this file).
826          */
827         case Pproc: {
828                 struct proc *p;
829                 struct proc_target *pt;
830
831                 p = PFIND(pfs->pfs_pid);
832                 if (p == NULL)
833                         break;
834                 if (!PRISON_CHECK(ap->a_cred, p->p_ucred))
835                         break;
836
837                 for (pt = &proc_targets[i];
838                      uio->uio_resid >= delen && i < nproc_targets; pt++, i++) {
839                         if (pt->pt_valid && (*pt->pt_valid)(p) == 0)
840                                 continue;
841
842                         dp->d_reclen = delen;
843                         dp->d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype);
844                         dp->d_namlen = pt->pt_namlen;
845                         bcopy(pt->pt_name, dp->d_name, pt->pt_namlen + 1);
846                         dp->d_type = pt->pt_type;
847
848                         if ((error = uiomove((caddr_t)dp, delen, uio)) != 0)
849                                 break;
850                 }
851
852                 break;
853             }
854
855         /*
856          * this is for the root of the procfs filesystem
857          * what is needed is a special entry for "curproc"
858          * followed by an entry for each process on allproc
859 #ifdef PROCFS_ZOMBIE
860          * and zombproc.
861 #endif
862          */
863
864         case Proot: {
865 #ifdef PROCFS_ZOMBIE
866                 int doingzomb = 0;
867 #endif
868                 int pcnt = 0;
869                 volatile struct proc *p = allproc.lh_first;
870
871                 for (; p && uio->uio_resid >= delen; i++, pcnt++) {
872                         bzero((char *) dp, delen);
873                         dp->d_reclen = delen;
874
875                         switch (i) {
876                         case 0:         /* `.' */
877                         case 1:         /* `..' */
878                                 dp->d_fileno = PROCFS_FILENO(0, Proot);
879                                 dp->d_namlen = i + 1;
880                                 bcopy("..", dp->d_name, dp->d_namlen);
881                                 dp->d_name[i + 1] = '\0';
882                                 dp->d_type = DT_DIR;
883                                 break;
884
885                         case 2:
886                                 dp->d_fileno = PROCFS_FILENO(0, Pcurproc);
887                                 dp->d_namlen = 7;
888                                 bcopy("curproc", dp->d_name, 8);
889                                 dp->d_type = DT_LNK;
890                                 break;
891
892                         default:
893                                 while (pcnt < i) {
894                                         p = p->p_list.le_next;
895                                         if (!p)
896                                                 goto done;
897                                         if (!PRISON_CHECK(ap->a_cred, p->p_ucred))
898                                                 continue;
899                                         pcnt++;
900                                 }
901                                 while (!PRISON_CHECK(ap->a_cred, p->p_ucred)) {
902                                         p = p->p_list.le_next;
903                                         if (!p)
904                                                 goto done;
905                                 }
906                                 dp->d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
907                                 dp->d_namlen = sprintf(dp->d_name, "%ld",
908                                     (long)p->p_pid);
909                                 dp->d_type = DT_DIR;
910                                 p = p->p_list.le_next;
911                                 break;
912                         }
913
914                         if ((error = uiomove((caddr_t)dp, delen, uio)) != 0)
915                                 break;
916                 }
917         done:
918
919 #ifdef PROCFS_ZOMBIE
920                 if (p == NULL && doingzomb == 0) {
921                         doingzomb = 1;
922                         p = zombproc.lh_first;
923                         goto again;
924                 }
925 #endif
926
927                 break;
928
929             }
930
931         default:
932                 error = ENOTDIR;
933                 break;
934         }
935
936         uio->uio_offset = i * delen;
937
938         return (error);
939 }
940
941 /*
942  * readlink reads the link of `curproc' or `file'
943  */
944 static int
945 procfs_readlink(ap)
946         struct vop_readlink_args *ap;
947 {
948         char buf[16];           /* should be enough */
949         struct proc *procp;
950         struct vnode *vp = ap->a_vp;
951         struct pfsnode *pfs = VTOPFS(vp);
952         char *fullpath, *freepath;
953         int error, len;
954
955         switch (pfs->pfs_type) {
956         case Pcurproc:
957                 if (pfs->pfs_fileno != PROCFS_FILENO(0, Pcurproc))
958                         return (EINVAL);
959
960                 len = snprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
961
962                 return (uiomove(buf, len, ap->a_uio));
963         /*
964          * There _should_ be no way for an entire process to disappear
965          * from under us...
966          */
967         case Pfile:
968                 procp = PFIND(pfs->pfs_pid);
969                 if (procp == NULL || procp->p_ucred == NULL) {
970                         printf("procfs_readlink: pid %d disappeared\n",
971                             pfs->pfs_pid);
972                         return (uiomove("unknown", sizeof("unknown") - 1,
973                             ap->a_uio));
974                 }
975                 error = textvp_fullpath(procp, &fullpath, &freepath);
976                 if (error != 0)
977                         return (uiomove("unknown", sizeof("unknown") - 1,
978                             ap->a_uio));
979                 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
980                 free(freepath, M_TEMP);
981                 return (error);
982         default:
983                 return (EINVAL);
984         }
985 }
986
987 /*
988  * convert decimal ascii to pid_t
989  */
990 static pid_t
991 atopid(b, len)
992         const char *b;
993         u_int len;
994 {
995         pid_t p = 0;
996
997         while (len--) {
998                 char c = *b++;
999                 if (c < '0' || c > '9')
1000                         return (NO_PID);
1001                 p = 10 * p + (c - '0');
1002                 if (p > PID_MAX)
1003                         return (NO_PID);
1004         }
1005
1006         return (p);
1007 }
1008
1009 /*
1010  * procfs vnode operations.
1011  */
1012 vop_t **procfs_vnodeop_p;
1013 static struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
1014         { &vop_default_desc,            (vop_t *) vop_defaultop },
1015         { &vop_access_desc,             (vop_t *) procfs_access },
1016         { &vop_advlock_desc,            (vop_t *) procfs_badop },
1017         { &vop_bmap_desc,               (vop_t *) procfs_bmap },
1018         { &vop_close_desc,              (vop_t *) procfs_close },
1019         { &vop_create_desc,             (vop_t *) procfs_badop },
1020         { &vop_getattr_desc,            (vop_t *) procfs_getattr },
1021         { &vop_inactive_desc,           (vop_t *) procfs_inactive },
1022         { &vop_link_desc,               (vop_t *) procfs_badop },
1023         { &vop_lookup_desc,             (vop_t *) procfs_lookup },
1024         { &vop_mkdir_desc,              (vop_t *) procfs_badop },
1025         { &vop_mknod_desc,              (vop_t *) procfs_badop },
1026         { &vop_open_desc,               (vop_t *) procfs_open },
1027         { &vop_pathconf_desc,           (vop_t *) vop_stdpathconf },
1028         { &vop_print_desc,              (vop_t *) procfs_print },
1029         { &vop_read_desc,               (vop_t *) procfs_rw },
1030         { &vop_readdir_desc,            (vop_t *) procfs_readdir },
1031         { &vop_readlink_desc,           (vop_t *) procfs_readlink },
1032         { &vop_reclaim_desc,            (vop_t *) procfs_reclaim },
1033         { &vop_remove_desc,             (vop_t *) procfs_badop },
1034         { &vop_rename_desc,             (vop_t *) procfs_badop },
1035         { &vop_rmdir_desc,              (vop_t *) procfs_badop },
1036         { &vop_setattr_desc,            (vop_t *) procfs_setattr },
1037         { &vop_symlink_desc,            (vop_t *) procfs_badop },
1038         { &vop_write_desc,              (vop_t *) procfs_rw },
1039         { &vop_ioctl_desc,              (vop_t *) procfs_ioctl },
1040         { NULL, NULL }
1041 };
1042 static struct vnodeopv_desc procfs_vnodeop_opv_desc =
1043         { &procfs_vnodeop_p, procfs_vnodeop_entries };
1044
1045 VNODEOP_SET(procfs_vnodeop_opv_desc);