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