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