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