#include <sys/param.h>
#include <sys/systm.h>
#include <sys/time.h>
#include <sys/kernel.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/vnode.h>
#include <sys/namei.h>
#include <sys/malloc.h>
#include <sys/dirent.h>
#include <sys/resourcevar.h>
#include <sys/ptrace.h>
#include <vm/vm.h>
#include <machine/reg.h>
#include <vfs/vfs_support.h>
#include <miscfs/procfs/procfs.h>
struct proc_target {
u_char pt_type;
u_char pt_namlen;
char *pt_name;
pfstype pt_pfstype;
int (*pt_valid) __P((struct proc *p));
} proc_targets[] = {
#define N(s) sizeof(s)-1, s
{ DT_DIR, N("."), Pproc, NULL },
{ DT_DIR, N(".."), Proot, NULL },
{ DT_REG, N("file"), Pfile, procfs_validfile },
{ DT_REG, N("mem"), Pmem, NULL },
{ DT_REG, N("regs"), Pregs, procfs_validregs },
{ DT_REG, N("fpregs"), Pfpregs, procfs_validfpregs },
{ DT_REG, N("ctl"), Pctl, NULL },
{ DT_REG, N("status"), Pstatus, NULL },
{ DT_REG, N("note"), Pnote, NULL },
{ DT_REG, N("notepg"), Pnotepg, NULL },
#undef N
};
static int nproc_targets = sizeof(proc_targets) / sizeof(proc_targets[0]);
static pid_t atopid __P((const char *, u_int));
procfs_open(ap)
struct vop_open_args *ap;
{
struct pfsnode *pfs = VTOPFS(ap->a_vp);
switch (pfs->pfs_type) {
case Pmem:
if (PFIND(pfs->pfs_pid) == 0)
return (ENOENT);
if ((pfs->pfs_flags & FWRITE) && (ap->a_mode & O_EXCL) ||
(pfs->pfs_flags & O_EXCL) && (ap->a_mode & FWRITE))
return (EBUSY);
if (ap->a_mode & FWRITE)
pfs->pfs_flags = ap->a_mode & (FWRITE|O_EXCL);
return (0);
default:
break;
}
return (0);
}
procfs_close(ap)
struct vop_close_args *ap;
{
struct pfsnode *pfs = VTOPFS(ap->a_vp);
switch (pfs->pfs_type) {
case Pmem:
if ((ap->a_fflag & FWRITE) && (pfs->pfs_flags & O_EXCL))
pfs->pfs_flags &= ~(FWRITE|O_EXCL);
break;
}
return (0);
}
procfs_ioctl(ap)
struct vop_ioctl_args *ap;
{
return (ENOTTY);
}
procfs_bmap(ap)
struct vop_bmap_args *ap;
{
if (ap->a_vpp != NULL)
*ap->a_vpp = ap->a_vp;
if (ap->a_bnp != NULL)
*ap->a_bnp = ap->a_bn;
return (0);
}
procfs_inactive(ap)
struct vop_inactive_args *ap;
{
struct pfsnode *pfs = VTOPFS(ap->a_vp);
if (PFIND(pfs->pfs_pid) == 0)
vgone(ap->a_vp);
return (0);
}
procfs_reclaim(ap)
struct vop_reclaim_args *ap;
{
return (procfs_freevp(ap->a_vp));
}
procfs_pathconf(ap)
struct vop_pathconf_args *ap;
{
switch (ap->a_name) {
case _PC_LINK_MAX:
*ap->a_retval = LINK_MAX;
return (0);
case _PC_MAX_CANON:
*ap->a_retval = MAX_CANON;
return (0);
case _PC_MAX_INPUT:
*ap->a_retval = MAX_INPUT;
return (0);
case _PC_PIPE_BUF:
*ap->a_retval = PIPE_BUF;
return (0);
case _PC_CHOWN_RESTRICTED:
*ap->a_retval = 1;
return (0);
case _PC_VDISABLE:
*ap->a_retval = _POSIX_VDISABLE;
return (0);
default:
return (EINVAL);
}
}
procfs_print(ap)
struct vop_print_args *ap;
{
struct pfsnode *pfs = VTOPFS(ap->a_vp);
printf("tag VT_PROCFS, type %s, pid %d, mode %x, flags %x\n",
pfs->pfs_type, pfs->pfs_pid, pfs->pfs_mode, pfs->pfs_flags);
}
procfs_abortop(ap)
struct vop_abortop_args *ap;
{
if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
FREE_ZONE(ap->a_cnp->cn_pnbuf, ap->a_cnp->cn_pnlen, M_NAMEI);
return (0);
}
procfs_badop()
{
return (EIO);
}
procfs_getattr(ap)
struct vop_getattr_args *ap;
{
struct pfsnode *pfs = VTOPFS(ap->a_vp);
struct vattr *vap = ap->a_vap;
struct proc *procp;
int error;
switch (pfs->pfs_type) {
case Proot:
case Pcurproc:
procp = 0;
break;
default:
procp = PFIND(pfs->pfs_pid);
if (procp == 0)
return (ENOENT);
}
error = 0;
VATTR_NULL(vap);
vap->va_type = ap->a_vp->v_type;
vap->va_mode = pfs->pfs_mode;
vap->va_fileid = pfs->pfs_fileno;
vap->va_flags = 0;
vap->va_blocksize = PAGE_SIZE;
vap->va_bytes = vap->va_size = 0;
microtime(&vap->va_ctime);
vap->va_atime = vap->va_mtime = vap->va_ctime;
switch (pfs->pfs_type) {
case Pmem:
case Pregs:
case Pfpregs:
if (procp->p_flag & P_SUGID)
vap->va_mode &= ~((VREAD|VWRITE)|
((VREAD|VWRITE)>>3)|
((VREAD|VWRITE)>>6));
case Pctl:
case Pstatus:
case Pnote:
case Pnotepg:
vap->va_nlink = 1;
vap->va_uid = procp->p_ucred->cr_uid;
vap->va_gid = procp->p_ucred->cr_gid;
break;
}
switch (pfs->pfs_type) {
case Proot:
vap->va_nlink = 1;
vap->va_uid = 0;
vap->va_gid = 0;
vap->va_size = vap->va_bytes = DEV_BSIZE;
break;
case Pcurproc: {
char buf[16];
vap->va_nlink = 1;
vap->va_uid = 0;
vap->va_gid = 0;
vap->va_size = vap->va_bytes =
sprintf(buf, "%ld", (long)curproc->p_pid);
break;
}
case Pproc:
vap->va_nlink = 2;
vap->va_uid = procp->p_ucred->cr_uid;
vap->va_gid = procp->p_ucred->cr_gid;
vap->va_size = vap->va_bytes = DEV_BSIZE;
break;
case Pfile:
error = EOPNOTSUPP;
break;
case Pmem:
vap->va_bytes = vap->va_size =
ctob(procp->p_vmspace->vm_tsize +
procp->p_vmspace->vm_dsize +
procp->p_vmspace->vm_ssize);
break;
#if defined(PT_GETREGS) || defined(PT_SETREGS)
case Pregs:
vap->va_bytes = vap->va_size = sizeof(struct reg);
break;
#endif
#if defined(PT_GETFPREGS) || defined(PT_SETFPREGS)
case Pfpregs:
vap->va_bytes = vap->va_size = sizeof(struct fpreg);
break;
#endif
case Pctl:
case Pstatus:
case Pnote:
case Pnotepg:
break;
default:
panic("procfs_getattr");
}
return (error);
}
procfs_setattr(ap)
struct vop_setattr_args *ap;
{
return (0);
}
procfs_access(ap)
struct vop_access_args *ap;
{
struct vattr va;
int error;
if (error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p))
return (error);
return (vaccess(va.va_mode, va.va_uid, va.va_gid, ap->a_mode,
ap->a_cred));
}
procfs_lookup(ap)
struct vop_lookup_args *ap;
{
struct componentname *cnp = ap->a_cnp;
struct vnode **vpp = ap->a_vpp;
struct vnode *dvp = ap->a_dvp;
char *pname = cnp->cn_nameptr;
struct proc_target *pt;
struct vnode *fvp;
pid_t pid;
struct pfsnode *pfs;
struct proc *p;
int i;
*vpp = NULL;
if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
return (EROFS);
if (cnp->cn_namelen == 1 && *pname == '.') {
*vpp = dvp;
VREF(dvp);
return (0);
}
pfs = VTOPFS(dvp);
switch (pfs->pfs_type) {
case Proot:
if (cnp->cn_flags & ISDOTDOT)
return (EIO);
if (CNEQ(cnp, "curproc", 7))
return (procfs_allocvp(dvp->v_mount, vpp, 0, Pcurproc));
pid = atopid(pname, cnp->cn_namelen);
if (pid == NO_PID)
break;
p = PFIND(pid);
if (p == 0)
break;
return (procfs_allocvp(dvp->v_mount, vpp, pid, Pproc));
case Pproc:
if (cnp->cn_flags & ISDOTDOT)
return (procfs_root(dvp->v_mount, vpp));
p = PFIND(pfs->pfs_pid);
if (p == 0)
break;
for (pt = proc_targets, i = 0; i < nproc_targets; pt++, i++) {
if (cnp->cn_namelen == pt->pt_namlen &&
bcmp(pt->pt_name, pname, cnp->cn_namelen) == 0 &&
(pt->pt_valid == NULL || (*pt->pt_valid)(p)))
goto found;
}
break;
found:
if (pt->pt_pfstype == Pfile) {
fvp = procfs_findtextvp(p);
VREF(fvp);
VOP_LOCK(fvp);
*vpp = fvp;
return (0);
}
return (procfs_allocvp(dvp->v_mount, vpp, pfs->pfs_pid,
pt->pt_pfstype));
default:
return (ENOTDIR);
}
return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
}
int
procfs_validfile(p)
struct proc *p;
{
return (procfs_findtextvp(p) != NULLVP);
}
procfs_readdir(ap)
struct vop_readdir_args *ap;
{
struct uio *uio = ap->a_uio;
struct pfsdent d;
struct pfsdent *dp = &d;
struct pfsnode *pfs;
int error;
int count;
int i;
if (ap->a_ncookies)
panic("procfs_readdir: not hungry");
pfs = VTOPFS(ap->a_vp);
if (uio->uio_resid < UIO_MX)
return (EINVAL);
if (uio->uio_offset & (UIO_MX-1))
return (EINVAL);
if (uio->uio_offset < 0)
return (EINVAL);
error = 0;
count = 0;
i = uio->uio_offset / UIO_MX;
switch (pfs->pfs_type) {
case Pproc: {
struct proc *p;
struct proc_target *pt;
p = PFIND(pfs->pfs_pid);
if (p == NULL)
break;
for (pt = &proc_targets[i];
uio->uio_resid >= UIO_MX && i < nproc_targets; pt++, i++) {
if (pt->pt_valid && (*pt->pt_valid)(p) == 0)
continue;
dp->d_reclen = UIO_MX;
dp->d_fileno = PROCFS_FILENO(pfs->pfs_pid, pt->pt_pfstype);
dp->d_namlen = pt->pt_namlen;
bcopy(pt->pt_name, dp->d_name, pt->pt_namlen + 1);
dp->d_type = pt->pt_type;
if (error = uiomove((caddr_t)dp, UIO_MX, uio))
break;
}
break;
}
case Proot: {
#ifdef PROCFS_ZOMBIE
int doingzomb = 0;
#endif
int pcnt = 0;
volatile struct proc *p = allproc.lh_first;
again:
for (; p && uio->uio_resid >= UIO_MX; i++, pcnt++) {
bzero((char *) dp, UIO_MX);
dp->d_reclen = UIO_MX;
switch (i) {
case 0:
case 1:
dp->d_fileno = PROCFS_FILENO(0, Proot);
dp->d_namlen = i + 1;
bcopy("..", dp->d_name, dp->d_namlen);
dp->d_name[i + 1] = '\0';
dp->d_type = DT_DIR;
break;
case 2:
dp->d_fileno = PROCFS_FILENO(0, Pcurproc);
dp->d_namlen = 7;
bcopy("curproc", dp->d_name, 8);
dp->d_type = DT_LNK;
break;
default:
while (pcnt < i) {
pcnt++;
p = p->p_list.le_next;
if (!p)
goto done;
}
dp->d_fileno = PROCFS_FILENO(p->p_pid, Pproc);
dp->d_namlen = sprintf(dp->d_name, "%ld",
(long)p->p_pid);
dp->d_type = DT_REG;
p = p->p_list.le_next;
break;
}
if (error = uiomove((caddr_t)dp, UIO_MX, uio))
break;
}
done:
#ifdef PROCFS_ZOMBIE
if (p == 0 && doingzomb == 0) {
doingzomb = 1;
p = zombproc.lh_first;
goto again;
}
#endif
break;
}
default:
error = ENOTDIR;
break;
}
uio->uio_offset = i * UIO_MX;
return (error);
}
procfs_readlink(ap)
struct vop_readlink_args *ap;
{
struct uio *uio = ap->a_uio;
char buf[16];
int len;
if (VTOPFS(ap->a_vp)->pfs_fileno != PROCFS_FILENO(0, Pcurproc))
return (EINVAL);
len = sprintf(buf, "%ld", (long)curproc->p_pid);
return (uiomove((caddr_t)buf, len, ap->a_uio));
}
static pid_t
atopid(b, len)
const char *b;
u_int len;
{
pid_t p = 0;
while (len--) {
char c = *b++;
if (c < '0' || c > '9')
return (NO_PID);
p = 10 * p + (c - '0');
if (p > PID_MAX)
return (NO_PID);
}
return (p);
}
#define VOPFUNC int (*)(void *)
int (**procfs_vnodeop_p)(void *);
struct vnodeopv_entry_desc procfs_vnodeop_entries[] = {
{ &vop_default_desc, (VOPFUNC)vn_default_error },
{ &vop_lookup_desc, (VOPFUNC)procfs_lookup },
{ &vop_create_desc, (VOPFUNC)procfs_create },
{ &vop_mknod_desc, (VOPFUNC)procfs_mknod },
{ &vop_open_desc, (VOPFUNC)procfs_open },
{ &vop_close_desc, (VOPFUNC)procfs_close },
{ &vop_access_desc, (VOPFUNC)procfs_access },
{ &vop_getattr_desc, (VOPFUNC)procfs_getattr },
{ &vop_setattr_desc, (VOPFUNC)procfs_setattr },
{ &vop_read_desc, (VOPFUNC)procfs_read },
{ &vop_write_desc, (VOPFUNC)procfs_write },
{ &vop_ioctl_desc, (VOPFUNC)procfs_ioctl },
{ &vop_select_desc, (VOPFUNC)procfs_select },
{ &vop_mmap_desc, (VOPFUNC)procfs_mmap },
{ &vop_fsync_desc, (VOPFUNC)procfs_fsync },
{ &vop_seek_desc, (VOPFUNC)procfs_seek },
{ &vop_remove_desc, (VOPFUNC)procfs_remove },
{ &vop_link_desc, (VOPFUNC)procfs_link },
{ &vop_rename_desc, (VOPFUNC)procfs_rename },
{ &vop_mkdir_desc, (VOPFUNC)procfs_mkdir },
{ &vop_rmdir_desc, (VOPFUNC)procfs_rmdir },
{ &vop_symlink_desc, (VOPFUNC)procfs_symlink },
{ &vop_readdir_desc, (VOPFUNC)procfs_readdir },
{ &vop_readlink_desc, (VOPFUNC)procfs_readlink },
{ &vop_abortop_desc, (VOPFUNC)procfs_abortop },
{ &vop_inactive_desc, (VOPFUNC)procfs_inactive },
{ &vop_reclaim_desc, (VOPFUNC)procfs_reclaim },
{ &vop_lock_desc, (VOPFUNC)procfs_lock },
{ &vop_unlock_desc, (VOPFUNC)procfs_unlock },
{ &vop_bmap_desc, (VOPFUNC)procfs_bmap },
{ &vop_strategy_desc, (VOPFUNC)procfs_strategy },
{ &vop_print_desc, (VOPFUNC)procfs_print },
{ &vop_islocked_desc, (VOPFUNC)procfs_islocked },
{ &vop_pathconf_desc, (VOPFUNC)procfs_pathconf },
{ &vop_advlock_desc, (VOPFUNC)procfs_advlock },
{ &vop_blkatoff_desc, (VOPFUNC)procfs_blkatoff },
{ &vop_valloc_desc, (VOPFUNC)procfs_valloc },
{ &vop_vfree_desc, (VOPFUNC)procfs_vfree },
{ &vop_truncate_desc, (VOPFUNC)procfs_truncate },
{ &vop_update_desc, (VOPFUNC)procfs_update },
{ &vop_copyfile_desc, (VOPFUNC)err_copyfile },
{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
};
struct vnodeopv_desc procfs_vnodeop_opv_desc =
{ &procfs_vnodeop_p, procfs_vnodeop_entries };