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