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