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