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>
68 #include <machine/limits.h>
70 extern struct vnode *procfs_findtextvp (struct proc *);
72 static int linprocfs_access (struct vop_access_args *);
73 static int linprocfs_badop (struct vop_generic_args *);
74 static int linprocfs_bmap (struct vop_bmap_args *);
75 static int linprocfs_close (struct vop_close_args *);
76 static int linprocfs_getattr (struct vop_getattr_args *);
77 static int linprocfs_inactive (struct vop_inactive_args *);
78 static int linprocfs_ioctl (struct vop_ioctl_args *);
79 static int linprocfs_lookup (struct vop_old_lookup_args *);
80 static int linprocfs_open (struct vop_open_args *);
81 static int linprocfs_print (struct vop_print_args *);
82 static int linprocfs_readdir (struct vop_readdir_args *);
83 static int linprocfs_readlink (struct vop_readlink_args *);
84 static int linprocfs_reclaim (struct vop_reclaim_args *);
85 static int linprocfs_setattr (struct vop_setattr_args *);
87 static int linprocfs_readdir_proc(struct vop_readdir_args *);
88 static int linprocfs_readdir_root(struct vop_readdir_args *);
89 static int linprocfs_readdir_net(struct vop_readdir_args *ap);
90 static int linprocfs_readdir_sys(struct vop_readdir_args *ap);
91 static int linprocfs_readdir_syskernel(struct vop_readdir_args *ap);
94 * procfs vnode operations.
96 struct vop_ops linprocfs_vnode_vops = {
97 .vop_default = vop_defaultop,
98 .vop_access = linprocfs_access,
99 .vop_advlock = (void *)linprocfs_badop,
100 .vop_bmap = linprocfs_bmap,
101 .vop_close = linprocfs_close,
102 .vop_old_create = (void *)linprocfs_badop,
103 .vop_getattr = linprocfs_getattr,
104 .vop_inactive = linprocfs_inactive,
105 .vop_old_link = (void *)linprocfs_badop,
106 .vop_old_lookup = linprocfs_lookup,
107 .vop_old_mkdir = (void *)linprocfs_badop,
108 .vop_old_mknod = (void *)linprocfs_badop,
109 .vop_open = linprocfs_open,
110 .vop_pathconf = vop_stdpathconf,
111 .vop_print = linprocfs_print,
112 .vop_read = (void *)linprocfs_rw,
113 .vop_readdir = linprocfs_readdir,
114 .vop_readlink = linprocfs_readlink,
115 .vop_reclaim = linprocfs_reclaim,
116 .vop_old_remove = (void *)linprocfs_badop,
117 .vop_old_rename = (void *)linprocfs_badop,
118 .vop_old_rmdir = (void *)linprocfs_badop,
119 .vop_setattr = linprocfs_setattr,
120 .vop_old_symlink = (void *)linprocfs_badop,
121 .vop_write = (void *)linprocfs_rw,
122 .vop_ioctl = linprocfs_ioctl
126 * This is a list of the valid names in the
127 * process-specific sub-directories. It is
128 * used in linprocfs_lookup and linprocfs_readdir
130 static struct proc_target {
135 int (*pt_valid) (struct proc *p);
137 #define N(s) sizeof(s)-1, s
138 /* name type validp */
139 { DT_DIR, N("."), Pproc, NULL },
140 { DT_DIR, N(".."), Proot, NULL },
141 { DT_REG, N("mem"), Pmem, NULL },
143 { DT_LNK, N("exe"), Pexe, NULL },
144 { DT_LNK, N("cwd"), Pcwd, NULL },
145 { DT_LNK, N("root"), Pprocroot, NULL },
146 { DT_LNK, N("fd"), Pfd, NULL },
148 { DT_REG, N("stat"), Pprocstat, NULL },
149 { DT_REG, N("status"), Pprocstatus, NULL },
150 { DT_REG, N("maps"), Pmaps, NULL },
151 { DT_REG, N("statm"), Pstatm, NULL },
153 { DT_REG, N("cmdline"), Pcmdline, NULL },
154 { DT_REG, N("environ"), Penviron, NULL },
158 static const int nproc_targets = NELEM(proc_targets);
160 static pid_t atopid (const char *, u_int);
163 * set things up for doing i/o on
164 * the pfsnode (vp). (vp) is locked
165 * on entry, and should be left locked
168 * for procfs we don't need to do anything
169 * in particular for i/o. all that is done
170 * is to support exclusive open on process
174 linprocfs_open(struct vop_open_args *ap)
176 struct pfsnode *pfs = VTOPFS(ap->a_vp);
179 p2 = PFIND(pfs->pfs_pid);
182 if (pfs->pfs_pid && !PRISON_CHECK(ap->a_cred, p2->p_ucred))
185 switch (pfs->pfs_type) {
187 if (((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL)) ||
188 ((pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE)))
191 if (p_trespass(ap->a_cred, p2->p_ucred))
194 if (ap->a_mode & FWRITE)
195 pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
202 return (vop_stdopen(ap));
206 * close the pfsnode (vp) after doing i/o.
207 * (vp) is not locked on entry or exit.
209 * nothing to do for procfs other than undo
210 * any exclusive open flag (see _open above).
213 linprocfs_close(struct vop_close_args *ap)
215 struct pfsnode *pfs = VTOPFS(ap->a_vp);
218 switch (pfs->pfs_type) {
220 if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
221 pfs->pfs_flags &= ~(FWRITE|O_EXCL);
223 * If this is the last close, then it checks to see if
224 * the target process has PF_LINGER set in p_pfsflags,
225 * if this is *not* the case, then the process' stop flags
226 * are cleared, and the process is woken up. This is
227 * to help prevent the case where a process has been
228 * told to stop on an event, but then the requesting process
229 * has gone away or forgotten about it.
232 if ((ap->a_vp->v_opencount < 2)
233 && (p = pfind(pfs->pfs_pid))
234 && !(p->p_pfsflags & PF_LINGER)) {
245 return (vop_stdclose(ap));
249 * do an ioctl operation on a pfsnode (vp).
250 * (vp) is not locked on entry or exit.
253 linprocfs_ioctl(struct vop_ioctl_args *ap)
255 struct pfsnode *pfs = VTOPFS(ap->a_vp);
259 struct procfs_status *psp;
262 procp = pfind(pfs->pfs_pid);
267 if (p_trespass(ap->a_cred, procp->p_ucred)) {
272 switch (ap->a_command) {
274 procp->p_stops |= *(unsigned int*)ap->a_data;
277 procp->p_stops &= ~*(unsigned int*)ap->a_data;
281 * NFLAGS is "non-suser_xxx flags" -- currently, only
282 * PFS_ISUGID ("ignore set u/g id");
284 #define NFLAGS (PF_ISUGID)
285 flags = (unsigned char)*(unsigned int*)ap->a_data;
286 if (flags & NFLAGS && (error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)))
288 procp->p_pfsflags = flags;
291 *(unsigned int*)ap->a_data = (unsigned int)procp->p_pfsflags;
293 psp = (struct procfs_status *)ap->a_data;
294 psp->state = (procp->p_step == 0);
295 psp->flags = procp->p_pfsflags;
296 psp->events = procp->p_stops;
298 psp->why = procp->p_stype;
299 psp->val = procp->p_xstat;
301 psp->why = psp->val = 0; /* Not defined values */
305 psp = (struct procfs_status *)ap->a_data;
306 spin_lock(&procp->p_spin);
307 if (procp->p_step == 0) {
308 spin_unlock(&procp->p_spin);
309 tsleep_interlock(&procp->p_stype, PCATCH);
310 if (procp->p_stops == 0) {
314 if (procp->p_flags & P_POSTEXIT) {
318 if (procp->p_flags & P_INEXEC) {
322 error = tsleep(&procp->p_stype, PCATCH | PINTERLOCKED,
327 spin_unlock(&procp->p_spin);
329 psp->state = 1; /* It stopped */
330 psp->flags = procp->p_pfsflags;
331 psp->events = procp->p_stops;
332 psp->why = procp->p_stype; /* why it stopped */
333 psp->val = procp->p_xstat; /* any extra info */
335 case PIOCCONT: /* Restart a proc */
336 if (procp->p_step == 0) {
337 error = EINVAL; /* Can only start a stopped process */
340 if ((signo = *(int*)ap->a_data) != 0) {
341 if (signo >= NSIG || signo <= 0) {
345 ksignal(procp, signo);
348 wakeup(&procp->p_step);
362 * do block mapping for pfsnode (vp).
363 * since we don't use the buffer cache
364 * for procfs this function should never
365 * be called. in any case, it's not clear
366 * what part of the kernel ever makes use
367 * of this function. for sanity, this is the
368 * usual no-op bmap, although returning
369 * (EIO) would be a reasonable alternative.
372 linprocfs_bmap(struct vop_bmap_args *ap)
374 if (ap->a_doffsetp != NULL)
375 *ap->a_doffsetp = ap->a_loffset;
376 if (ap->a_runp != NULL)
378 if (ap->a_runb != NULL)
384 * linprocfs_inactive is called when the pfsnode
385 * is vrele'd and the reference count is about
386 * to go to zero. (vp) will be on the vnode free
387 * list, so to get it back vget() must be
390 * (vp) is locked on entry and must remain locked
394 linprocfs_inactive(struct vop_inactive_args *ap)
396 /*struct vnode *vp = ap->a_vp;*/
402 * _reclaim is called when getnewvnode()
403 * wants to make use of an entry on the vnode
404 * free list. at this time the filesystem needs
405 * to free any private data and remove the node
406 * from any private lists.
409 linprocfs_reclaim(struct vop_reclaim_args *ap)
411 return (linprocfs_freevp(ap->a_vp));
415 * _print is used for debugging.
416 * just print a readable description
420 linprocfs_print(struct vop_print_args *ap)
422 struct pfsnode *pfs = VTOPFS(ap->a_vp);
424 kprintf("tag VT_PROCFS, type %d, pid %ld, mode %x, flags %lx\n",
425 pfs->pfs_type, (long)pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
430 * generic entry point for unsupported operations
433 linprocfs_badop(struct vop_generic_args *ap __unused)
440 * Invent attributes for pfsnode (vp) and store
442 * Directories lengths are returned as zero since
443 * any real length would require the genuine size
444 * to be computed, and nothing cares anyway.
446 * this is relatively minimal for procfs.
449 linprocfs_getattr(struct vop_getattr_args *ap)
451 struct pfsnode *pfs = VTOPFS(ap->a_vp);
452 struct vattr *vap = ap->a_vap;
457 * First make sure that the process and its credentials
460 switch (pfs->pfs_type) {
467 procp = PFIND(pfs->pfs_pid);
468 if (procp == NULL || procp->p_ucred == NULL)
474 /* start by zeroing out the attributes */
477 /* next do all the common fields */
478 vap->va_type = ap->a_vp->v_type;
479 vap->va_mode = pfs->pfs_mode;
480 vap->va_fileid = pfs->pfs_fileno;
482 vap->va_blocksize = PAGE_SIZE;
483 vap->va_bytes = vap->va_size = 0;
484 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
487 * Make all times be current TOD.
488 * It would be possible to get the process start
489 * time from the p_stat structure, but there's
490 * no "file creation" time stamp anyway, and the
491 * p_stat structure is not addressible if u. gets
492 * swapped out for that process.
494 nanotime(&vap->va_ctime);
495 vap->va_atime = vap->va_mtime = vap->va_ctime;
498 * now do the object specific fields
500 * The size could be set from struct reg, but it's hardly
501 * worth the trouble, and it puts some (potentially) machine
502 * dependent data into this machine-independent code. If it
503 * becomes important then this function should break out into
504 * a per-file stat function in the corresponding .c file.
509 vap->va_uid = procp->p_ucred->cr_uid;
510 vap->va_gid = procp->p_ucred->cr_gid;
513 switch (pfs->pfs_type) {
519 * Set nlink to 1 to tell fts(3) we don't actually know.
524 vap->va_size = vap->va_bytes = DEV_BSIZE;
528 char buf[16]; /* should be enough */
531 vap->va_size = vap->va_bytes =
532 ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
537 vap->va_nlink = nproc_targets;
538 vap->va_size = vap->va_bytes = DEV_BSIZE;
542 char *fullpath, *freepath;
543 error = cache_fullpath(procp, &procp->p_textnch, &fullpath, &freepath, 0);
544 /* error = vn_fullpath(procp, NULL, &fullpath, &freepath); */
546 vap->va_size = strlen(fullpath);
547 kfree(freepath, M_TEMP);
549 vap->va_size = sizeof("unknown") - 1;
552 vap->va_bytes = vap->va_size;
556 char *fullpath, *freepath;
557 error = cache_fullpath(procp, &procp->p_fd->fd_ncdir, &fullpath, &freepath, 0);
559 vap->va_size = strlen(fullpath);
560 kfree(freepath, M_TEMP);
562 vap->va_size = sizeof("unknown") - 1;
565 vap->va_bytes = vap->va_size;
569 struct nchandle *nchp;
570 char *fullpath, *freepath;
571 nchp = jailed(procp->p_ucred) ? &procp->p_fd->fd_njdir : &procp->p_fd->fd_nrdir;
572 error = cache_fullpath(procp, nchp, &fullpath, &freepath, 0);
574 vap->va_size = strlen(fullpath);
575 kfree(freepath, M_TEMP);
577 vap->va_size = sizeof("unknown") - 1;
580 vap->va_bytes = vap->va_size;
584 if (procp == curproc) {
585 vap->va_size = sizeof("/dev/fd") - 1;
588 vap->va_size = sizeof("unknown") - 1;
591 vap->va_bytes = vap->va_size;
607 vap->va_bytes = vap->va_size = 0;
614 * If we denied owner access earlier, then we have to
615 * change the owner to root - otherwise 'ps' and friends
616 * will break even though they are setgid kmem. *SIGH*
618 if (procp->p_flags & P_SUGID)
621 vap->va_uid = procp->p_ucred->cr_uid;
630 vap->va_bytes = vap->va_size = 0;
631 /* uid, gid are already set */
635 panic("linprocfs_getattr");
642 linprocfs_setattr(struct vop_setattr_args *ap)
645 if (ap->a_vap->va_flags != VNOVAL)
649 * just fake out attribute setting
650 * it's not good to generate an error
651 * return, otherwise things like creat()
652 * will fail when they try to set the
653 * file length to 0. worse, this means
654 * that echo $note > /proc/$pid/note will fail.
661 * implement access checking.
663 * something very similar to this code is duplicated
664 * throughout the 4bsd kernel and should be moved
665 * into kern/vfs_subr.c sometime.
667 * actually, the check for super-user is slightly
668 * broken since it will allow read access to write-only
669 * objects. this doesn't cause any particular trouble
670 * but does mean that the i/o entry points need to check
671 * that the operation really does make sense.
674 linprocfs_access(struct vop_access_args *ap)
681 * If you're the super-user,
682 * you always get access.
684 if (ap->a_cred->cr_uid == 0)
688 error = VOP_GETATTR(ap->a_vp, vap);
693 * Access check is based on only one of owner, group, public.
694 * If not owner, then check group. If not a member of the
695 * group, then check public access.
697 if (ap->a_cred->cr_uid != vap->va_uid) {
702 gp = ap->a_cred->cr_groups;
703 for (i = 0; i < ap->a_cred->cr_ngroups; i++, gp++)
704 if (vap->va_gid == *gp)
711 if ((vap->va_mode & ap->a_mode) == ap->a_mode)
718 * lookup. this is incredibly complicated in the general case, however
719 * for most pseudo-filesystems very little needs to be done.
722 linprocfs_lookup(struct vop_old_lookup_args *ap)
724 struct componentname *cnp = ap->a_cnp;
725 struct vnode **vpp = ap->a_vpp;
726 struct vnode *dvp = ap->a_dvp;
727 char *pname = cnp->cn_nameptr;
728 struct proc_target *pt;
737 if (cnp->cn_nameiop == NAMEI_DELETE ||
738 cnp->cn_nameiop == NAMEI_RENAME ||
739 cnp->cn_nameiop == NAMEI_CREATE) {
745 if (cnp->cn_namelen == 1 && *pname == '.') {
752 switch (pfs->pfs_type) {
754 if (cnp->cn_flags & CNP_ISDOTDOT) {
755 error = linprocfs_root(dvp->v_mount, vpp);
758 if (CNEQ(cnp, "kernel", 6)) {
759 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Psyskernel);
764 if (cnp->cn_flags & CNP_ISDOTDOT) {
765 error = linprocfs_root(dvp->v_mount, vpp);
768 if (CNEQ(cnp, "dev", 3)) {
769 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pnetdev);
774 if (cnp->cn_flags & CNP_ISDOTDOT) {
775 /* XXX: this is wrong, wrong, wrong. */
776 error = linprocfs_root(dvp->v_mount, vpp);
779 if (CNEQ(cnp, "osrelease", 9)) {
780 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Posrelease);
783 if (CNEQ(cnp, "ostype", 6)) {
784 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Postype);
787 if (CNEQ(cnp, "pid_max", 7)) {
788 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Ppidmax);
791 if (CNEQ(cnp, "version", 7)) {
792 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pversion);
798 if (cnp->cn_flags & CNP_ISDOTDOT)
801 if (CNEQ(cnp, "self", 4)) {
802 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pself);
805 if (CNEQ(cnp, "meminfo", 7)) {
806 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pmeminfo);
809 if (CNEQ(cnp, "cpuinfo", 7)) {
810 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pcpuinfo);
813 if (CNEQ(cnp, "mounts", 6)) {
814 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pmounts);
817 if (CNEQ(cnp, "stat", 4)) {
818 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pstat);
821 if (CNEQ(cnp, "uptime", 6)) {
822 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Puptime);
825 if (CNEQ(cnp, "version", 7)) {
826 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pversion);
829 if (CNEQ(cnp, "loadavg", 7)) {
830 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Ploadavg);
833 if (CNEQ(cnp, "net", 3)) {
834 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Pnet);
837 if (CNEQ(cnp, "sys", 3)) {
838 error = linprocfs_allocvp(dvp->v_mount, vpp, 0, Psys);
842 pid = atopid(pname, cnp->cn_namelen);
850 if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred))
853 if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 &&
854 ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid)
857 error = linprocfs_allocvp(dvp->v_mount, vpp, pid, Pproc);
861 if (cnp->cn_flags & CNP_ISDOTDOT) {
862 error = linprocfs_root(dvp->v_mount, vpp);
866 p = PFIND(pfs->pfs_pid);
870 if (!PRISON_CHECK(ap->a_cnp->cn_cred, p->p_ucred))
873 if (ps_showallprocs == 0 && ap->a_cnp->cn_cred->cr_uid != 0 &&
874 ap->a_cnp->cn_cred->cr_uid != p->p_ucred->cr_uid)
877 for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
878 if (cnp->cn_namelen == pt->pt_namlen &&
879 bcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
880 (pt->pt_valid == NULL || (*pt->pt_valid)(p)))
886 error = linprocfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
895 if (cnp->cn_nameiop == NAMEI_LOOKUP)
901 * If no error occured *vpp will hold a referenced locked vnode.
902 * dvp was passed to us locked and *vpp must be returned locked
903 * so if dvp != *vpp and CNP_LOCKPARENT is not set, unlock dvp.
907 if (*vpp != dvp && (cnp->cn_flags & CNP_LOCKPARENT) == 0) {
908 cnp->cn_flags |= CNP_PDIRUNLOCK;
916 * Does this process have a text file?
919 linprocfs_validfile(struct proc *p)
922 return (procfs_findtextvp(p) != NULLVP);
926 * readdir() returns directory entries from pfsnode (vp).
928 * We generate just one directory entry at a time, as it would probably
929 * not pay off to buffer several entries locally to save uiomove calls.
931 * linprocfs_readdir(struct vnode *a_vp, struct uio *a_uio,
932 * struct ucred *a_cred, int *a_eofflag,
933 * int *a_ncookies, off_t **a_cookies)
936 linprocfs_readdir(struct vop_readdir_args *ap)
941 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
944 pfs = VTOPFS(ap->a_vp);
945 if ((error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
948 switch (pfs->pfs_type) {
951 * This is for the process-specific sub-directories.
952 * all that is needed to is copy out all the entries
953 * from the procent[] table (top of this file).
955 error = linprocfs_readdir_proc(ap);
959 * This is for the root of the procfs filesystem
960 * what is needed is a special entry for "self"
961 * followed by an entry for each process on allproc
963 error = linprocfs_readdir_root(ap);
966 error = linprocfs_readdir_net(ap);
969 error = linprocfs_readdir_sys(ap);
972 error = linprocfs_readdir_syskernel(ap);
984 linprocfs_readdir_proc(struct vop_readdir_args *ap)
987 int error, i, retval;
989 struct proc_target *pt;
990 struct uio *uio = ap->a_uio;
992 pfs = VTOPFS(ap->a_vp);
993 p = PFIND(pfs->pfs_pid);
996 if (!PRISON_CHECK(ap->a_cred, p->p_ucred))
1000 i = uio->uio_offset;
1002 for (pt = &proc_targets[i];
1003 !error && uio->uio_resid > 0 && i < nproc_targets; pt++, i++) {
1004 if (pt->pt_valid && (*pt->pt_valid)(p) == 0)
1007 retval = vop_write_dirent(&error, uio,
1008 PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype), pt->pt_type,
1009 pt->pt_namlen, pt->pt_name);
1014 uio->uio_offset = i;
1019 struct linprocfs_readdir_root_info {
1028 * Scan the root directory by scanning all process
1030 static int linprocfs_readdir_root_callback(struct proc *p, void *data);
1033 linprocfs_readdir_root(struct vop_readdir_args *ap)
1035 struct linprocfs_readdir_root_info info;
1036 struct uio *uio = ap->a_uio;
1040 info.i = uio->uio_offset;
1043 info.cred = ap->a_cred;
1045 while (info.pcnt < 12) {
1046 res = linprocfs_readdir_root_callback(NULL, &info);
1051 allproc_scan(linprocfs_readdir_root_callback, &info);
1053 uio->uio_offset = info.i;
1058 linprocfs_readdir_root_callback(struct proc *p, void *data)
1060 struct linprocfs_readdir_root_info *info = data;
1062 struct uio *uio = info->uio;
1065 char d_name_pid[20];
1069 switch (info->pcnt) {
1071 d_ino = PROCFS_FILENO(0, Proot);
1077 d_ino = PROCFS_FILENO(0, Proot);
1084 d_ino = PROCFS_FILENO(0, Proot);
1091 d_ino = PROCFS_FILENO(0, Pmeminfo);
1098 d_ino = PROCFS_FILENO(0, Pcpuinfo);
1105 d_ino = PROCFS_FILENO(0, Pstat);
1112 d_ino = PROCFS_FILENO(0, Puptime);
1119 d_ino = PROCFS_FILENO(0, Pversion);
1126 d_ino = PROCFS_FILENO(0, Ploadavg);
1132 d_ino = PROCFS_FILENO(0, Pnet);
1138 d_ino = PROCFS_FILENO(0, Psys);
1144 d_ino = PROCFS_FILENO(0, Pmounts);
1151 d_ino = PROCFS_FILENO(0, Pdevices);
1159 * Ignore processes that aren't in our prison
1161 if (PRISON_CHECK(info->cred, p->p_ucred) == 0)
1165 * Ignore processes that we do not want to be visible.
1167 if (ps_showallprocs == 0 &&
1168 info->cred->cr_uid != 0 &&
1169 info->cred->cr_uid != p->p_ucred->cr_uid) {
1174 * Skip processes we have already read (optimization)
1176 if (info->pcnt < info->i) {
1180 d_ino = PROCFS_FILENO(p->p_pid, Pproc);
1181 d_namlen = ksnprintf(d_name_pid, sizeof(d_name_pid),
1182 "%ld", (long)p->p_pid);
1183 d_name = d_name_pid;
1189 * Skip processes we have already read
1191 if (info->pcnt < info->i) {
1195 retval = vop_write_dirent(&info->error, info->uio,
1196 d_ino, d_type, d_namlen, d_name);
1198 ++info->pcnt; /* iterate proc candidates scanned */
1199 ++info->i; /* iterate entries written */
1201 if (retval || info->error || uio->uio_resid <= 0)
1207 * Scan the root directory by scanning all process
1209 static int linprocfs_readdir_net_callback(struct proc *p, void *data);
1212 linprocfs_readdir_net(struct vop_readdir_args *ap)
1214 struct linprocfs_readdir_root_info info;
1215 struct uio *uio = ap->a_uio;
1219 info.i = uio->uio_offset;
1222 info.cred = ap->a_cred;
1224 while (info.pcnt < 3) {
1225 res = linprocfs_readdir_net_callback(NULL, &info);
1230 uio->uio_offset = info.i;
1235 linprocfs_readdir_net_callback(struct proc *p, void *data)
1237 struct linprocfs_readdir_root_info *info = data;
1239 struct uio *uio = info->uio;
1245 switch (info->pcnt) {
1247 d_ino = PROCFS_FILENO(0, Pnet);
1253 d_ino = PROCFS_FILENO(0, Proot);
1260 d_ino = PROCFS_FILENO(0, Pnet);
1274 * Skip processes we have already read
1276 if (info->pcnt < info->i) {
1280 retval = vop_write_dirent(&info->error, info->uio,
1281 d_ino, d_type, d_namlen, d_name);
1283 ++info->pcnt; /* iterate proc candidates scanned */
1284 ++info->i; /* iterate entries written */
1286 if (retval || info->error || uio->uio_resid <= 0)
1298 * Scan the root directory by scanning all process
1300 static int linprocfs_readdir_sys_callback(struct proc *p, void *data);
1303 linprocfs_readdir_sys(struct vop_readdir_args *ap)
1305 struct linprocfs_readdir_root_info info;
1306 struct uio *uio = ap->a_uio;
1310 info.i = uio->uio_offset;
1313 info.cred = ap->a_cred;
1315 while (info.pcnt < 3) {
1316 res = linprocfs_readdir_sys_callback(NULL, &info);
1321 uio->uio_offset = info.i;
1326 linprocfs_readdir_sys_callback(struct proc *p, void *data)
1328 struct linprocfs_readdir_root_info *info = data;
1330 struct uio *uio = info->uio;
1336 switch (info->pcnt) {
1338 d_ino = PROCFS_FILENO(0, Psys);
1344 d_ino = PROCFS_FILENO(0, Proot);
1351 d_ino = PROCFS_FILENO(0, Psyskernel);
1365 * Skip processes we have already read
1367 if (info->pcnt < info->i) {
1371 retval = vop_write_dirent(&info->error, info->uio,
1372 d_ino, d_type, d_namlen, d_name);
1374 ++info->pcnt; /* iterate proc candidates scanned */
1375 ++info->i; /* iterate entries written */
1377 if (retval || info->error || uio->uio_resid <= 0)
1387 * Scan the root directory by scanning all process
1389 static int linprocfs_readdir_syskernel_callback(struct proc *p, void *data);
1392 linprocfs_readdir_syskernel(struct vop_readdir_args *ap)
1394 struct linprocfs_readdir_root_info info;
1395 struct uio *uio = ap->a_uio;
1399 info.i = uio->uio_offset;
1402 info.cred = ap->a_cred;
1404 while (info.pcnt < 6) {
1405 res = linprocfs_readdir_syskernel_callback(NULL, &info);
1410 uio->uio_offset = info.i;
1415 linprocfs_readdir_syskernel_callback(struct proc *p, void *data)
1417 struct linprocfs_readdir_root_info *info = data;
1419 struct uio *uio = info->uio;
1425 switch (info->pcnt) {
1427 d_ino = PROCFS_FILENO(0, Psyskernel);
1433 d_ino = PROCFS_FILENO(0, Psys);
1440 d_ino = PROCFS_FILENO(0, Posrelease);
1442 d_name = "osrelease";
1447 d_ino = PROCFS_FILENO(0, Postype);
1454 d_ino = PROCFS_FILENO(0, Pversion);
1461 d_ino = PROCFS_FILENO(0, Ppidmax);
1475 * Skip processes we have already read
1477 if (info->pcnt < info->i) {
1481 retval = vop_write_dirent(&info->error, info->uio,
1482 d_ino, d_type, d_namlen, d_name);
1484 ++info->pcnt; /* iterate proc candidates scanned */
1485 ++info->i; /* iterate entries written */
1487 if (retval || info->error || uio->uio_resid <= 0)
1493 * readlink reads the link of `self' or `exe'
1496 linprocfs_readlink(struct vop_readlink_args *ap)
1498 char buf[16]; /* should be enough */
1500 struct vnode *vp = ap->a_vp;
1501 struct nchandle *nchp;
1502 struct pfsnode *pfs = VTOPFS(vp);
1503 char *fullpath, *freepath;
1506 switch (pfs->pfs_type) {
1508 if (pfs->pfs_fileno != PROCFS_FILENO(0, Pself))
1511 len = ksnprintf(buf, sizeof(buf), "%ld", (long)curproc->p_pid);
1513 return (uiomove(buf, len, ap->a_uio));
1515 * There _should_ be no way for an entire process to disappear
1519 procp = PFIND(pfs->pfs_pid);
1520 if (procp == NULL || procp->p_ucred == NULL) {
1521 kprintf("linprocfs_readlink: pid %d disappeared\n",
1523 return (uiomove("unknown", sizeof("unknown") - 1,
1526 error = cache_fullpath(procp, &procp->p_textnch, &fullpath, &freepath, 0);
1528 return (uiomove("unknown", sizeof("unknown") - 1,
1530 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1531 kfree(freepath, M_TEMP);
1534 procp = PFIND(pfs->pfs_pid);
1535 if (procp == NULL || procp->p_ucred == NULL) {
1536 kprintf("linprocfs_readlink: pid %d disappeared\n",
1538 return (uiomove("unknown", sizeof("unknown") - 1,
1541 error = cache_fullpath(procp, &procp->p_fd->fd_ncdir, &fullpath, &freepath, 0);
1543 return (uiomove("unknown", sizeof("unknown") - 1,
1545 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1546 kfree(freepath, M_TEMP);
1549 procp = PFIND(pfs->pfs_pid);
1550 if (procp == NULL || procp->p_ucred == NULL) {
1551 kprintf("linprocfs_readlink: pid %d disappeared\n",
1553 return (uiomove("unknown", sizeof("unknown") - 1,
1556 nchp = jailed(procp->p_ucred) ? &procp->p_fd->fd_njdir : &procp->p_fd->fd_nrdir;
1557 error = cache_fullpath(procp, nchp, &fullpath, &freepath, 0);
1559 return (uiomove("unknown", sizeof("unknown") - 1,
1561 error = uiomove(fullpath, strlen(fullpath), ap->a_uio);
1562 kfree(freepath, M_TEMP);
1565 procp = PFIND(pfs->pfs_pid);
1566 if (procp == NULL || procp->p_ucred == NULL) {
1567 kprintf("linprocfs_readlink: pid %d disappeared\n",
1569 return (uiomove("unknown", sizeof("unknown") - 1,
1572 if (procp == curproc) {
1573 return (uiomove("/dev/fd", sizeof("/dev/fd") - 1,
1576 return (uiomove("unknown", sizeof("unknown") - 1,
1587 * convert decimal ascii to pid_t
1590 atopid(const char *b, u_int len)
1596 if (c < '0' || c > '9')
1598 p = 10 * p + (c - '0');