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.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
39 * @(#)procfs_vnops.c 8.18 (Berkeley) 5/21/95
41 * $FreeBSD: src/sys/i386/linux/linprocfs/linprocfs_vnops.c,v 1.3.2.5 2001/08/12 14:29:19 rwatson Exp $
45 * procfs vnode interface
48 #include <sys/param.h>
49 #include <sys/systm.h>
51 #include <sys/kernel.h>
53 #include <sys/fcntl.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>
64 #include <vm/vm_zone.h>
65 #include "linprocfs.h"
66 #include <sys/pioctl.h>
67 #include <sys/spinlock2.h>
69 #include <machine/limits.h>
71 extern struct vnode *procfs_findtextvp (struct proc *);
73 static int linprocfs_access (struct vop_access_args *);
74 static int linprocfs_badop (struct vop_generic_args *);
75 static int linprocfs_bmap (struct vop_bmap_args *);
76 static int linprocfs_close (struct vop_close_args *);
77 static int linprocfs_getattr (struct vop_getattr_args *);
78 static int linprocfs_inactive (struct vop_inactive_args *);
79 static int linprocfs_ioctl (struct vop_ioctl_args *);
80 static int linprocfs_lookup (struct vop_old_lookup_args *);
81 static int linprocfs_open (struct vop_open_args *);
82 static int linprocfs_print (struct vop_print_args *);
83 static int linprocfs_readdir (struct vop_readdir_args *);
84 static int linprocfs_readlink (struct vop_readlink_args *);
85 static int linprocfs_reclaim (struct vop_reclaim_args *);
86 static int linprocfs_setattr (struct vop_setattr_args *);
88 static int linprocfs_readdir_proc(struct vop_readdir_args *);
89 static int linprocfs_readdir_root(struct vop_readdir_args *);
90 static int linprocfs_readdir_net(struct vop_readdir_args *ap);
91 static int linprocfs_readdir_sys(struct vop_readdir_args *ap);
92 static int linprocfs_readdir_syskernel(struct vop_readdir_args *ap);
95 * procfs vnode operations.
97 struct vop_ops linprocfs_vnode_vops = {
98 .vop_default = vop_defaultop,
99 .vop_access = linprocfs_access,
100 .vop_advlock = (void *)linprocfs_badop,
101 .vop_bmap = linprocfs_bmap,
102 .vop_close = linprocfs_close,
103 .vop_old_create = (void *)linprocfs_badop,
104 .vop_getattr = linprocfs_getattr,
105 .vop_inactive = linprocfs_inactive,
106 .vop_old_link = (void *)linprocfs_badop,
107 .vop_old_lookup = linprocfs_lookup,
108 .vop_old_mkdir = (void *)linprocfs_badop,
109 .vop_old_mknod = (void *)linprocfs_badop,
110 .vop_open = linprocfs_open,
111 .vop_pathconf = vop_stdpathconf,
112 .vop_print = linprocfs_print,
113 .vop_read = (void *)linprocfs_rw,
114 .vop_readdir = linprocfs_readdir,
115 .vop_readlink = linprocfs_readlink,
116 .vop_reclaim = linprocfs_reclaim,
117 .vop_old_remove = (void *)linprocfs_badop,
118 .vop_old_rename = (void *)linprocfs_badop,
119 .vop_old_rmdir = (void *)linprocfs_badop,
120 .vop_setattr = linprocfs_setattr,
121 .vop_old_symlink = (void *)linprocfs_badop,
122 .vop_write = (void *)linprocfs_rw,
123 .vop_ioctl = linprocfs_ioctl
127 * This is a list of the valid names in the
128 * process-specific sub-directories. It is
129 * used in linprocfs_lookup and linprocfs_readdir
131 static struct proc_target {
136 int (*pt_valid) (struct proc *p);
138 #define N(s) sizeof(s)-1, s
139 /* name type validp */
140 { DT_DIR, N("."), Pproc, NULL },
141 { DT_DIR, N(".."), Proot, NULL },
142 { DT_REG, N("mem"), Pmem, NULL },
144 { DT_LNK, N("exe"), Pexe, NULL },
145 { DT_LNK, N("cwd"), Pcwd, NULL },
146 { DT_LNK, N("root"), Pprocroot, NULL },
147 { DT_LNK, N("fd"), Pfd, NULL },
149 { DT_REG, N("stat"), Pprocstat, NULL },
150 { DT_REG, N("status"), Pprocstatus, NULL },
151 { DT_REG, N("maps"), Pmaps, NULL },
152 { DT_REG, N("statm"), Pstatm, NULL },
154 { DT_REG, N("cmdline"), Pcmdline, NULL },
155 { DT_REG, N("environ"), Penviron, NULL },
159 static const int nproc_targets = NELEM(proc_targets);
161 static pid_t atopid (const char *, u_int);
164 * set things up for doing i/o on
165 * the pfsnode (vp). (vp) is locked
166 * on entry, and should be left locked
169 * for procfs we don't need to do anything
170 * in particular for i/o. all that is done
171 * is to support exclusive open on process
175 linprocfs_open(struct vop_open_args *ap)
177 struct pfsnode *pfs = VTOPFS(ap->a_vp);
180 p2 = PFIND(pfs->pfs_pid);
183 if (pfs->pfs_pid && !PRISON_CHECK(ap->a_cred, p2->p_ucred))
186 switch (pfs->pfs_type) {
188 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
189 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
192 if (p_trespass(ap->a_cred, p2->p_ucred))
195 if (ap->a_mode & FWRITE)
196 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
203 return (vop_stdopen(ap));
207 * close the pfsnode (vp) after doing i/o.
208 * (vp) is not locked on entry or exit.
210 * nothing to do for procfs other than undo
211 * any exclusive open flag (see _open above).
214 linprocfs_close(struct vop_close_args *ap)
216 struct pfsnode *pfs = VTOPFS(ap->a_vp);
219 switch (pfs->pfs_type) {
221 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
222 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
224 * If this is the last close, then it checks to see if
225 * the target process has PF_LINGER set in p_pfsflags,
226 * if this is *not* the case, then the process' stop flags
227 * are cleared, and the process is woken up. This is
228 * to help prevent the case where a process has been
229 * told to stop on an event, but then the requesting process
230 * has gone away or forgotten about it.
233 if ((ap->a_vp->v_opencount < 2)
234 && (p = pfind(pfs->pfs_pid))
235 && !(p->p_pfsflags & PF_LINGER)) {
246 return (vop_stdclose(ap));
250 * do an ioctl operation on a pfsnode (vp).
251 * (vp) is not locked on entry or exit.
254 linprocfs_ioctl(struct vop_ioctl_args *ap)
256 struct pfsnode *pfs = VTOPFS(ap->a_vp);
260 struct procfs_status *psp;
263 procp = pfind(pfs->pfs_pid);
268 if (p_trespass(ap->a_cred, procp->p_ucred)) {
273 switch (ap->a_command) {
275 procp->p_stops |= *(unsigned int*)ap->a_data;
278 procp->p_stops &= ~*(unsigned int*)ap->a_data;
282 * NFLAGS is "non-suser_xxx flags" -- currently, only
283 * PFS_ISUGID ("ignore set u/g id");
285 #define NFLAGS (PF_ISUGID)
286 flags = (unsigned char)*(unsigned int*)ap->a_data;
287 if (flags & NFLAGS && (error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)))
289 procp->p_pfsflags = flags;
292 *(unsigned int*)ap->a_data = (unsigned int)procp->p_pfsflags;
294 psp = (struct procfs_status *)ap->a_data;
295 psp->state = (procp->p_step == 0);
296 psp->flags = procp->p_pfsflags;
297 psp->events = procp->p_stops;
299 psp->why = procp->p_stype;
300 psp->val = procp->p_xstat;
302 psp->why = psp->val = 0; /* Not defined values */
306 psp = (struct procfs_status *)ap->a_data;
307 spin_lock(&procp->p_spin);
308 if (procp->p_step == 0) {
309 spin_unlock(&procp->p_spin);
310 tsleep_interlock(&procp->p_stype, PCATCH);
311 if (procp->p_stops == 0) {
315 if (procp->p_flags & P_POSTEXIT) {
319 if (procp->p_flags & P_INEXEC) {
323 error = tsleep(&procp->p_stype, PCATCH | PINTERLOCKED,
328 spin_unlock(&procp->p_spin);
330 psp->state = 1; /* It stopped */
331 psp->flags = procp->p_pfsflags;
332 psp->events = procp->p_stops;
333 psp->why = procp->p_stype; /* why it stopped */
334 psp->val = procp->p_xstat; /* any extra info */
336 case PIOCCONT: /* Restart a proc */
337 if (procp->p_step == 0) {
338 error = EINVAL; /* Can only start a stopped process */
341 if ((signo = *(int*)ap->a_data) != 0) {
342 if (signo >= NSIG || signo <= 0) {
346 ksignal(procp, signo);
349 wakeup(&procp->p_step);
363 * do block mapping for pfsnode (vp).
364 * since we don't use the buffer cache
365 * for procfs this function should never
366 * be called. in any case, it's not clear
367 * what part of the kernel ever makes use
368 * of this function. for sanity, this is the
369 * usual no-op bmap, although returning
370 * (EIO) would be a reasonable alternative.
373 linprocfs_bmap(struct vop_bmap_args *ap)
375 if (ap->a_doffsetp != NULL)
376 *ap->a_doffsetp = ap->a_loffset;
377 if (ap->a_runp != NULL)
379 if (ap->a_runb != NULL)
385 * linprocfs_inactive is called when the pfsnode
386 * is vrele'd and the reference count is about
387 * to go to zero. (vp) will be on the vnode free
388 * list, so to get it back vget() must be
391 * (vp) is locked on entry and must remain locked
395 linprocfs_inactive(struct vop_inactive_args *ap)
397 /*struct vnode *vp = ap->a_vp;*/
403 * _reclaim is called when getnewvnode()
404 * wants to make use of an entry on the vnode
405 * free list. at this time the filesystem needs
406 * to free any private data and remove the node
407 * from any private lists.
410 linprocfs_reclaim(struct vop_reclaim_args *ap)
412 return (linprocfs_freevp(ap->a_vp));
416 * _print is used for debugging.
417 * just print a readable description
421 linprocfs_print(struct vop_print_args *ap)
423 struct pfsnode *pfs = VTOPFS(ap->a_vp);
425 kprintf("tag VT_PROCFS, type %d, pid %ld, mode %x, flags %lx\n",
426 pfs->pfs_type, (long)pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
431 * generic entry point for unsupported operations
434 linprocfs_badop(struct vop_generic_args *ap __unused)
441 * Invent attributes for pfsnode (vp) and store
443 * Directories lengths are returned as zero since
444 * any real length would require the genuine size
445 * to be computed, and nothing cares anyway.
447 * this is relatively minimal for procfs.
450 linprocfs_getattr(struct vop_getattr_args *ap)
452 struct pfsnode *pfs = VTOPFS(ap->a_vp);
453 struct vattr *vap = ap->a_vap;
458 * First make sure that the process and its credentials
461 switch (pfs->pfs_type) {
468 procp = PFIND(pfs->pfs_pid);
469 if (procp == NULL || procp->p_ucred == NULL)
475 /* start by zeroing out the attributes */
478 /* next do all the common fields */
479 vap->va_type = ap->a_vp->v_type;
480 vap->va_mode = pfs->pfs_mode;
481 vap->va_fileid = pfs->pfs_fileno;
483 vap->va_blocksize = PAGE_SIZE;
484 vap->va_bytes = vap->va_size = 0;
485 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
488 * Make all times be current TOD.
489 * It would be possible to get the process start
490 * time from the p_stat structure, but there's
491 * no "file creation" time stamp anyway, and the
492 * p_stat structure is not addressible if u. gets
493 * swapped out for that process.
495 nanotime(&vap->va_ctime);
496 vap->va_atime = vap->va_mtime = vap->va_ctime;
499 * now do the object specific fields
501 * The size could be set from struct reg, but it's hardly
502 * worth the trouble, and it puts some (potentially) machine
503 * dependent data into this machine-independent code. If it
504 * becomes important then this function should break out into
505 * a per-file stat function in the corresponding .c file.
510 vap->va_uid = procp->p_ucred->cr_uid;
511 vap->va_gid = procp->p_ucred->cr_gid;
514 switch (pfs->pfs_type) {
520 * Set nlink to 1 to tell fts(3) we don't actually know.
525 vap->va_size = vap->va_bytes = DEV_BSIZE;
529 char buf[16]; /* should be enough */
532 vap->va_size = vap->va_bytes =
533 ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
538 vap->va_nlink = nproc_targets;
539 vap->va_size = vap->va_bytes = DEV_BSIZE;
543 char *fullpath, *freepath;
544 error = cache_fullpath(procp, &procp->p_textnch, &fullpath, &freepath, 0);
545 /* error = vn_fullpath(procp, NULL, &fullpath, &freepath); */
547 vap->va_size = strlen(fullpath);
548 kfree(freepath, M_TEMP);
550 vap->va_size = sizeof("unknown") - 1;
553 vap->va_bytes = vap->va_size;
557 char *fullpath, *freepath;
558 error = cache_fullpath(procp, &procp->p_fd->fd_ncdir, &fullpath, &freepath, 0);
560 vap->va_size = strlen(fullpath);
561 kfree(freepath, M_TEMP);
563 vap->va_size = sizeof("unknown") - 1;
566 vap->va_bytes = vap->va_size;
570 struct nchandle *nchp;
571 char *fullpath, *freepath;
572 nchp = jailed(procp->p_ucred) ? &procp->p_fd->fd_njdir : &procp->p_fd->fd_nrdir;
573 error = cache_fullpath(procp, nchp, &fullpath, &freepath, 0);
575 vap->va_size = strlen(fullpath);
576 kfree(freepath, M_TEMP);
578 vap->va_size = sizeof("unknown") - 1;
581 vap->va_bytes = vap->va_size;
585 if (procp == curproc) {
586 vap->va_size = sizeof("/dev/fd") - 1;
589 vap->va_size = sizeof("unknown") - 1;
592 vap->va_bytes = vap->va_size;
608 vap->va_bytes = vap->va_size = 0;
615 * If we denied owner access earlier, then we have to
616 * change the owner to root - otherwise 'ps' and friends
617 * will break even though they are setgid kmem. *SIGH*
619 if (procp->p_flags & P_SUGID)
622 vap->va_uid = procp->p_ucred->cr_uid;
631 vap->va_bytes = vap->va_size = 0;
632 /* uid, gid are already set */
636 panic("linprocfs_getattr");
643 linprocfs_setattr(struct vop_setattr_args *ap)
646 if (ap->a_vap->va_flags != VNOVAL)
650 * just fake out attribute setting
651 * it's not good to generate an error
652 * return, otherwise things like creat()
653 * will fail when they try to set the
654 * file length to 0. worse, this means
655 * that echo $note > /proc/$pid/note will fail.
662 * implement access checking.
664 * something very similar to this code is duplicated
665 * throughout the 4bsd kernel and should be moved
666 * into kern/vfs_subr.c sometime.
668 * actually, the check for super-user is slightly
669 * broken since it will allow read access to write-only
670 * objects. this doesn't cause any particular trouble
671 * but does mean that the i/o entry points need to check
672 * that the operation really does make sense.
675 linprocfs_access(struct vop_access_args *ap)
682 * If you're the super-user,
683 * you always get access.
685 if (ap->a_cred->cr_uid == 0)
689 error = VOP_GETATTR(ap->a_vp, vap);
694 * Access check is based on only one of owner, group, public.
695 * If not owner, then check group. If not a member of the
696 * group, then check public access.
698 if (ap->a_cred->cr_uid != vap->va_uid) {
703 gp = ap->a_cred->cr_groups;
704 for (i = 0; i < ap->a_cred->cr_ngroups; i++, gp++)
705 if (vap->va_gid == *gp)
712 if ((vap->va_mode & ap->a_mode) == ap->a_mode)
719 * lookup. this is incredibly complicated in the general case, however
720 * for most pseudo-filesystems very little needs to be done.
723 linprocfs_lookup(struct vop_old_lookup_args *ap)
725 struct componentname *cnp = ap->a_cnp;
726 struct vnode **vpp = ap->a_vpp;
727 struct vnode *dvp = ap->a_dvp;
728 char *pname = cnp->cn_nameptr;
729 struct proc_target *pt;
738 if (cnp->cn_nameiop == NAMEI_DELETE ||
739 cnp->cn_nameiop == NAMEI_RENAME ||
740 cnp->cn_nameiop == NAMEI_CREATE) {
746 if (cnp->cn_namelen == 1 && *pname == '.') {
753 switch (pfs->pfs_type) {
755 if (cnp->cn_flags & CNP_ISDOTDOT) {
756 error = linprocfs_root(dvp->v_mount, vpp);
759 if (CNEQ(cnp, "kernel", 6)) {
760 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Psyskernel);
765 if (cnp->cn_flags & CNP_ISDOTDOT) {
766 error = linprocfs_root(dvp->v_mount, vpp);
769 if (CNEQ(cnp, "dev", 3)) {
770 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pnetdev);
775 if (cnp->cn_flags & CNP_ISDOTDOT) {
776 /* XXX: this is wrong, wrong, wrong. */
777 error = linprocfs_root(dvp->v_mount, vpp);
780 if (CNEQ(cnp, "osrelease", 9)) {
781 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Posrelease);
784 if (CNEQ(cnp, "ostype", 6)) {
785 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Postype);
788 if (CNEQ(cnp, "pid_max", 7)) {
789 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Ppidmax);
792 if (CNEQ(cnp, "version", 7)) {
793 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pversion);
799 if (cnp->cn_flags & CNP_ISDOTDOT)
802 if (CNEQ(cnp, "self", 4)) {
803 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pself);
806 if (CNEQ(cnp, "meminfo", 7)) {
807 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pmeminfo);
810 if (CNEQ(cnp, "cpuinfo", 7)) {
811 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pcpuinfo);
814 if (CNEQ(cnp, "mounts", 6)) {
815 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pmounts);
818 if (CNEQ(cnp, "stat", 4)) {
819 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pstat);
822 if (CNEQ(cnp, "uptime", 6)) {
823 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Puptime);
826 if (CNEQ(cnp, "version", 7)) {
827 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pversion);
830 if (CNEQ(cnp, "loadavg", 7)) {
831 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Ploadavg);
834 if (CNEQ(cnp, "net", 3)) {
835 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pnet);
838 if (CNEQ(cnp, "sys", 3)) {
839 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Psys);
843 pid = atopid(pname, cnp->cn_namelen);
851 if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred))
854 if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 &&
855 ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid)
858 error = linprocfs_allocvp(dvp->v_mount, vpp, pid, Pproc);
862 if (cnp->cn_flags & CNP_ISDOTDOT) {
863 error = linprocfs_root(dvp->v_mount, vpp);
867 p = PFIND(pfs->pfs_pid);
871 if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred))
874 if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 &&
875 ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid)
878 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
879 if (cnp->cn_namelen == pt->pt_namlen &&
880 bcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
881 (pt->pt_valid == NULL || (*pt->pt_valid)(p)))
887 error = linprocfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
896 if (cnp->cn_nameiop == NAMEI_LOOKUP)
902 * If no error occured *vpp will hold a referenced locked vnode.
903 * dvp was passed to us locked and *vpp must be returned locked
904 * so if dvp != *vpp and CNP_LOCKPARENT is not set, unlock dvp.
908 if (*vpp != dvp && (cnp->cn_flags & CNP_LOCKPARENT) == 0) {
909 cnp->cn_flags |= CNP_PDIRUNLOCK;
917 * Does this process have a text file?
920 linprocfs_validfile(struct proc *p)
923 return (procfs_findtextvp(p) != NULLVP);
927 * readdir() returns directory entries from pfsnode (vp).
929 * We generate just one directory entry at a time, as it would probably
930 * not pay off to buffer several entries locally to save uiomove calls.
932 * linprocfs_readdir(struct vnode *a_vp, struct uio *a_uio,
933 * struct ucred *a_cred, int *a_eofflag,
934 * int *a_ncookies, off_t **a_cookies)
937 linprocfs_readdir(struct vop_readdir_args *ap)
942 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
945 pfs = VTOPFS(ap->a_vp);
946 if ((error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
949 switch (pfs->pfs_type) {
952 * This is for the process-specific sub-directories.
953 * all that is needed to is copy out all the entries
954 * from the procent[] table (top of this file).
956 error = linprocfs_readdir_proc(ap);
960 * This is for the root of the procfs filesystem
961 * what is needed is a special entry for "self"
962 * followed by an entry for each process on allproc
964 error = linprocfs_readdir_root(ap);
967 error = linprocfs_readdir_net(ap);
970 error = linprocfs_readdir_sys(ap);
973 error = linprocfs_readdir_syskernel(ap);
985 linprocfs_readdir_proc(struct vop_readdir_args *ap)
988 int error, i, retval;
990 struct proc_target *pt;
991 struct uio *uio = ap->a_uio;
993 pfs = VTOPFS(ap->a_vp);
994 p = PFIND(pfs->pfs_pid);
997 if (!PRISON_CHECK(ap->a_cred, p->p_ucred))
1001 i = uio->uio_offset;
1003 for (pt = &proc_targets[i];
1004 !error && uio->uio_resid > 0 && i < nproc_targets; pt++, i++) {
1005 if (pt->pt_valid && (*pt->pt_valid)(p) == 0)
1008 retval = vop_write_dirent(&error, uio,
1009 PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype), pt->pt_type,
1010 pt->pt_namlen, pt->pt_name);
1015 uio->uio_offset = i;
1020 struct linprocfs_readdir_root_info {
1029 * Scan the root directory by scanning all process
1031 static int linprocfs_readdir_root_callback(struct proc *p, void *data);
1034 linprocfs_readdir_root(struct vop_readdir_args *ap)
1036 struct linprocfs_readdir_root_info info;
1037 struct uio *uio = ap->a_uio;
1041 info.i = uio->uio_offset;
1044 info.cred = ap->a_cred;
1046 while (info.pcnt < 12) {
1047 res = linprocfs_readdir_root_callback(NULL, &info);
1052 allproc_scan(linprocfs_readdir_root_callback, &info);
1054 uio->uio_offset = info.i;
1059 linprocfs_readdir_root_callback(struct proc *p, void *data)
1061 struct linprocfs_readdir_root_info *info = data;
1063 struct uio *uio = info->uio;
1066 char d_name_pid[20];
1070 switch (info->pcnt) {
1072 d_ino = PROCFS_FILENO(0, Proot);
1078 d_ino = PROCFS_FILENO(0, Proot);
1085 d_ino = PROCFS_FILENO(0, Proot);
1092 d_ino = PROCFS_FILENO(0, Pmeminfo);
1099 d_ino = PROCFS_FILENO(0, Pcpuinfo);
1106 d_ino = PROCFS_FILENO(0, Pstat);
1113 d_ino = PROCFS_FILENO(0, Puptime);
1120 d_ino = PROCFS_FILENO(0, Pversion);
1127 d_ino = PROCFS_FILENO(0, Ploadavg);
1133 d_ino = PROCFS_FILENO(0, Pnet);
1139 d_ino = PROCFS_FILENO(0, Psys);
1145 d_ino = PROCFS_FILENO(0, Pmounts);
1152 d_ino = PROCFS_FILENO(0, Pdevices);
1160 * Ignore processes that aren't in our prison
1162 if (PRISON_CHECK(info->cred, p->p_ucred) == 0)
1166 * Ignore processes that we do not want to be visible.
1168 if (ps_showallprocs == 0 &&
1169 info->cred->cr_uid != 0 &&
1170 info->cred->cr_uid != p->p_ucred->cr_uid) {
1175 * Skip processes we have already read (optimization)
1177 if (info->pcnt < info->i) {
1181 d_ino = PROCFS_FILENO(p->p_pid, Pproc);
1182 d_namlen = ksnprintf(d_name_pid, sizeof(d_name_pid),
1183 "%ld", (long)p->p_pid);
1184 d_name = d_name_pid;
1190 * Skip processes we have already read
1192 if (info->pcnt < info->i) {
1196 retval = vop_write_dirent(&info->error, info->uio,
1197 d_ino, d_type, d_namlen, d_name);
1199 ++info->pcnt; /* iterate proc candidates scanned */
1200 ++info->i; /* iterate entries written */
1202 if (retval || info->error || uio->uio_resid <= 0)
1208 * Scan the root directory by scanning all process
1210 static int linprocfs_readdir_net_callback(struct proc *p, void *data);
1213 linprocfs_readdir_net(struct vop_readdir_args *ap)
1215 struct linprocfs_readdir_root_info info;
1216 struct uio *uio = ap->a_uio;
1220 info.i = uio->uio_offset;
1223 info.cred = ap->a_cred;
1225 while (info.pcnt < 3) {
1226 res = linprocfs_readdir_net_callback(NULL, &info);
1231 uio->uio_offset = info.i;
1236 linprocfs_readdir_net_callback(struct proc *p, void *data)
1238 struct linprocfs_readdir_root_info *info = data;
1240 struct uio *uio = info->uio;
1246 switch (info->pcnt) {
1248 d_ino = PROCFS_FILENO(0, Pnet);
1254 d_ino = PROCFS_FILENO(0, Proot);
1261 d_ino = PROCFS_FILENO(0, Pnet);
1275 * Skip processes we have already read
1277 if (info->pcnt < info->i) {
1281 retval = vop_write_dirent(&info->error, info->uio,
1282 d_ino, d_type, d_namlen, d_name);
1284 ++info->pcnt; /* iterate proc candidates scanned */
1285 ++info->i; /* iterate entries written */
1287 if (retval || info->error || uio->uio_resid <= 0)
1299 * Scan the root directory by scanning all process
1301 static int linprocfs_readdir_sys_callback(struct proc *p, void *data);
1304 linprocfs_readdir_sys(struct vop_readdir_args *ap)
1306 struct linprocfs_readdir_root_info info;
1307 struct uio *uio = ap->a_uio;
1311 info.i = uio->uio_offset;
1314 info.cred = ap->a_cred;
1316 while (info.pcnt < 3) {
1317 res = linprocfs_readdir_sys_callback(NULL, &info);
1322 uio->uio_offset = info.i;
1327 linprocfs_readdir_sys_callback(struct proc *p, void *data)
1329 struct linprocfs_readdir_root_info *info = data;
1331 struct uio *uio = info->uio;
1337 switch (info->pcnt) {
1339 d_ino = PROCFS_FILENO(0, Psys);
1345 d_ino = PROCFS_FILENO(0, Proot);
1352 d_ino = PROCFS_FILENO(0, Psyskernel);
1366 * Skip processes we have already read
1368 if (info->pcnt < info->i) {
1372 retval = vop_write_dirent(&info->error, info->uio,
1373 d_ino, d_type, d_namlen, d_name);
1375 ++info->pcnt; /* iterate proc candidates scanned */
1376 ++info->i; /* iterate entries written */
1378 if (retval || info->error || uio->uio_resid <= 0)
1388 * Scan the root directory by scanning all process
1390 static int linprocfs_readdir_syskernel_callback(struct proc *p, void *data);
1393 linprocfs_readdir_syskernel(struct vop_readdir_args *ap)
1395 struct linprocfs_readdir_root_info info;
1396 struct uio *uio = ap->a_uio;
1400 info.i = uio->uio_offset;
1403 info.cred = ap->a_cred;
1405 while (info.pcnt < 6) {
1406 res = linprocfs_readdir_syskernel_callback(NULL, &info);
1411 uio->uio_offset = info.i;
1416 linprocfs_readdir_syskernel_callback(struct proc *p, void *data)
1418 struct linprocfs_readdir_root_info *info = data;
1420 struct uio *uio = info->uio;
1426 switch (info->pcnt) {
1428 d_ino = PROCFS_FILENO(0, Psyskernel);
1434 d_ino = PROCFS_FILENO(0, Psys);
1441 d_ino = PROCFS_FILENO(0, Posrelease);
1443 d_name = "osrelease";
1448 d_ino = PROCFS_FILENO(0, Postype);
1455 d_ino = PROCFS_FILENO(0, Pversion);
1462 d_ino = PROCFS_FILENO(0, Ppidmax);
1476 * Skip processes we have already read
1478 if (info->pcnt < info->i) {
1482 retval = vop_write_dirent(&info->error, info->uio,
1483 d_ino, d_type, d_namlen, d_name);
1485 ++info->pcnt; /* iterate proc candidates scanned */
1486 ++info->i; /* iterate entries written */
1488 if (retval || info->error || uio->uio_resid <= 0)
1494 * readlink reads the link of `self' or `exe'
1497 linprocfs_readlink(struct vop_readlink_args *ap)
1499 char buf[16]; /* should be enough */
1501 struct vnode *vp = ap->a_vp;
1502 struct nchandle *nchp;
1503 struct pfsnode *pfs = VTOPFS(vp);
1504 char *fullpath, *freepath;
1507 switch (pfs->pfs_type) {
1509 if (pfs->pfs_fileno != PROCFS_FILENO(0, Pself))
1512 len = ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
1514 return (uiomove(buf, len, ap->a_uio));
1516 * There _should_ be no way for an entire process to disappear
1520 procp = PFIND(pfs->pfs_pid);
1521 if (procp == NULL || procp->p_ucred == NULL) {
1522 kprintf("linprocfs_readlink: pid %d disappeared\n",
1524 return (uiomove("unknown", sizeof("unknown") - 1,
1527 error = cache_fullpath(procp, &procp->p_textnch, &fullpath, &freepath, 0);
1529 return (uiomove("unknown", sizeof("unknown") - 1,
1531 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1532 kfree(freepath, M_TEMP);
1535 procp = PFIND(pfs->pfs_pid);
1536 if (procp == NULL || procp->p_ucred == NULL) {
1537 kprintf("linprocfs_readlink: pid %d disappeared\n",
1539 return (uiomove("unknown", sizeof("unknown") - 1,
1542 error = cache_fullpath(procp, &procp->p_fd->fd_ncdir, &fullpath, &freepath, 0);
1544 return (uiomove("unknown", sizeof("unknown") - 1,
1546 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1547 kfree(freepath, M_TEMP);
1550 procp = PFIND(pfs->pfs_pid);
1551 if (procp == NULL || procp->p_ucred == NULL) {
1552 kprintf("linprocfs_readlink: pid %d disappeared\n",
1554 return (uiomove("unknown", sizeof("unknown") - 1,
1557 nchp = jailed(procp->p_ucred) ? &procp->p_fd->fd_njdir : &procp->p_fd->fd_nrdir;
1558 error = cache_fullpath(procp, nchp, &fullpath, &freepath, 0);
1560 return (uiomove("unknown", sizeof("unknown") - 1,
1562 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1563 kfree(freepath, M_TEMP);
1566 procp = PFIND(pfs->pfs_pid);
1567 if (procp == NULL || procp->p_ucred == NULL) {
1568 kprintf("linprocfs_readlink: pid %d disappeared\n",
1570 return (uiomove("unknown", sizeof("unknown") - 1,
1573 if (procp == curproc) {
1574 return (uiomove("/dev/fd", sizeof("/dev/fd") - 1,
1577 return (uiomove("unknown", sizeof("unknown") - 1,
1588 * convert decimal ascii to pid_t
1591 atopid(const char *b, u_int len)
1597 if (c < '0' || c > '9')
1599 p = 10 * p + (c - '0');