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