calls to chdirec didn't pass 'p'.
[unix-history] / usr / src / sys / kern / kern_sysctl.c
index cbc8fcf..d4d8dc0 100644 (file)
@@ -2,51 +2,41 @@
  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  *
  *
- *     @(#)kern_sysctl.c       7.6 (Berkeley) %G%
+ *     @(#)kern_sysctl.c       7.21 (Berkeley) %G%
  */
 
 #include "param.h"
  */
 
 #include "param.h"
-#include "user.h"
 #include "proc.h"
 #include "proc.h"
-#include "text.h"
 #include "kinfo.h"
 #include "kinfo.h"
-#include "vm.h"
 #include "ioctl.h"
 #include "tty.h"
 #include "buf.h"
 #include "ioctl.h"
 #include "tty.h"
 #include "buf.h"
+#include "file.h"
 
 
+#include "vm/vm.h"
+
+#include "kinfo_proc.h"
 
 #define snderr(e) { error = (e); goto release;}
 
 #define snderr(e) { error = (e); goto release;}
-extern int kinfo_doproc(), kinfo_rtable();
+extern int kinfo_doproc(), kinfo_rtable(), kinfo_vnode(), kinfo_file();
+extern int kinfo_meter(), kinfo_loadavg(), kinfo_clockrate();
 struct kinfo_lock kinfo_lock;
 
 struct kinfo_lock kinfo_lock;
 
-getkerninfo()
-{
-       register struct a {
+/* ARGSUSED */
+getkerninfo(p, uap, retval)
+       struct proc *p;
+       register struct args {
                int     op;
                char    *where;
                int     *size;
                int     arg;
                int     op;
                char    *where;
                int     *size;
                int     arg;
-       } *uap = (struct a *)u.u_ap;
-
-       int     bufsize,        /* max size of users buffer */
-               needed, locked, (*server)(), error = 0;
-
-       if (error = copyin((caddr_t)uap->size,
-                               (caddr_t)&bufsize, sizeof (bufsize)))
-               goto done;
+       } *uap;
+       int *retval;
+{
+       int bufsize;            /* max size of users buffer */
+       int needed, locked, (*server)(), error = 0;
 
        switch (ki_type(uap->op)) {
 
 
        switch (ki_type(uap->op)) {
 
@@ -58,6 +48,26 @@ getkerninfo()
                server = kinfo_rtable;
                break;
 
                server = kinfo_rtable;
                break;
 
+       case KINFO_VNODE:
+               server = kinfo_vnode;
+               break;
+
+       case KINFO_FILE:
+               server = kinfo_file;
+               break;
+
+       case KINFO_METER:
+               server = kinfo_meter;
+               break;
+
+       case KINFO_LOADAVG:
+               server = kinfo_loadavg;
+               break;
+
+       case KINFO_CLOCKRATE:
+               server = kinfo_clockrate;
+               break;
+
        default:
                error = EINVAL;
                goto done;
        default:
                error = EINVAL;
                goto done;
@@ -66,37 +76,37 @@ getkerninfo()
                error = (*server)(uap->op, NULL, NULL, uap->arg, &needed);
                goto done;
        }
                error = (*server)(uap->op, NULL, NULL, uap->arg, &needed);
                goto done;
        }
+       if (error = copyin((caddr_t)uap->size, (caddr_t)&bufsize,
+           sizeof (bufsize)))
+               goto done;
        while (kinfo_lock.kl_lock) {
        while (kinfo_lock.kl_lock) {
-               kinfo_lock.kl_want++;
-               sleep(&kinfo_lock, PRIBIO+1);
-               kinfo_lock.kl_want--;
+               kinfo_lock.kl_want = 1;
+               sleep((caddr_t)&kinfo_lock, PRIBIO+1);
                kinfo_lock.kl_locked++;
        }
                kinfo_lock.kl_locked++;
        }
-       kinfo_lock.kl_lock++;
+       kinfo_lock.kl_lock = 1;
 
        if (!useracc(uap->where, bufsize, B_WRITE))
                snderr(EFAULT);
 
        if (!useracc(uap->where, bufsize, B_WRITE))
                snderr(EFAULT);
-       /*
-        * lock down target pages - NEED DEADLOCK AVOIDANCE
-        */
-       if (bufsize > ((int)ptob(freemem) - (20 * 1024)))       /* XXX */
-               snderr(ENOMEM);
-       vslock(uap->where, bufsize);
+       if (server != kinfo_vnode)      /* XXX */
+               vslock(uap->where, bufsize);
        locked = bufsize;
        error = (*server)(uap->op, uap->where, &bufsize, uap->arg, &needed);
        locked = bufsize;
        error = (*server)(uap->op, uap->where, &bufsize, uap->arg, &needed);
-       vsunlock(uap->where, locked, B_WRITE);
+       if (server != kinfo_vnode)      /* XXX */
+               vsunlock(uap->where, locked, B_WRITE);
        if (error == 0)
                error = copyout((caddr_t)&bufsize,
                                (caddr_t)uap->size, sizeof (bufsize));
 release:
        if (error == 0)
                error = copyout((caddr_t)&bufsize,
                                (caddr_t)uap->size, sizeof (bufsize));
 release:
-       kinfo_lock.kl_lock--;
-       if (kinfo_lock.kl_want)
-               wakeup(&kinfo_lock);
+       kinfo_lock.kl_lock = 0;
+       if (kinfo_lock.kl_want) {
+               kinfo_lock.kl_want = 0;
+               wakeup((caddr_t)&kinfo_lock);
+       }
 done:
 done:
-       if (error)
-               u.u_error = error;
-       else
-               u.u_r.r_val1 = needed;
+       if (!error)
+               *retval = needed;
+       return (error);
 }
 
 /* 
 }
 
 /* 
@@ -105,21 +115,18 @@ done:
 #define KINFO_PROCSLOP (5 * sizeof (struct kinfo_proc))
 
 kinfo_doproc(op, where, acopysize, arg, aneeded)
 #define KINFO_PROCSLOP (5 * sizeof (struct kinfo_proc))
 
 kinfo_doproc(op, where, acopysize, arg, aneeded)
+       int op;
        char *where;
        char *where;
-       int *acopysize, *aneeded;
+       int *acopysize, arg, *aneeded;
 {
        register struct proc *p;
 {
        register struct proc *p;
-       register caddr_t dp = (caddr_t)where;
+       register struct kinfo_proc *dp = (struct kinfo_proc *)where;
        register needed = 0;
        register needed = 0;
-       int buflen;
+       int buflen = where != NULL ? *acopysize : 0;
        int doingzomb;
        struct eproc eproc;
        int doingzomb;
        struct eproc eproc;
-       struct tty *tp;
        int error = 0;
 
        int error = 0;
 
-       if (where != NULL)
-               buflen = *acopysize;
-
        p = allproc;
        doingzomb = 0;
 again:
        p = allproc;
        doingzomb = 0;
 again:
@@ -150,52 +157,24 @@ again:
                        break;
 
                case KINFO_PROC_UID:
                        break;
 
                case KINFO_PROC_UID:
-                       if (p->p_uid != (uid_t)arg)
+                       if (p->p_ucred->cr_uid != (uid_t)arg)
                                continue;
                        break;
 
                case KINFO_PROC_RUID:
                                continue;
                        break;
 
                case KINFO_PROC_RUID:
-                       if (p->p_ruid != (uid_t)arg)
+                       if (p->p_cred->p_ruid != (uid_t)arg)
                                continue;
                        break;
                }
                                continue;
                        break;
                }
-               if (where != NULL && buflen >= sizeof (struct kinfo_proc)) {
-                       register struct text *txt;
-
-                       if (error = copyout((caddr_t)p, dp, 
+               if (buflen >= sizeof (struct kinfo_proc)) {
+                       fill_eproc(p, &eproc);
+                       if (error = copyout((caddr_t)p, &dp->kp_proc, 
                            sizeof (struct proc)))
                                return (error);
                            sizeof (struct proc)))
                                return (error);
-                       dp += sizeof (struct proc);
-                       /*
-                        *      XXX NEED ALLIGNMENT
-                        */
-                       eproc.e_paddr = p;
-                       eproc.e_sess = p->p_pgrp->pg_session;
-                       eproc.e_pgid = p->p_pgrp->pg_id;
-                       eproc.e_jobc = p->p_pgrp->pg_jobc;
-                       tp = p->p_pgrp->pg_session->s_ttyp;
-                       if ((p->p_flag&SCTTY) && tp != NULL) {
-                               eproc.e_tdev = tp->t_dev;
-                               eproc.e_tpgid = tp->t_pgrp ? 
-                                       tp->t_pgrp->pg_id : -1;
-                               eproc.e_tsess = tp->t_session;
-                       } else
-                               eproc.e_tdev = NODEV;
-                       if (p->p_wmesg)
-                               strncpy(eproc.e_wmesg, p->p_wmesg, WMESGLEN);
-                       if (txt = p->p_textp) {
-                               eproc.e_xsize = txt->x_size;
-                               eproc.e_xrssize = txt->x_rssize;
-                               eproc.e_xccount = txt->x_ccount;
-                               eproc.e_xswrss = txt->x_swrss;
-                       } else {
-                               eproc.e_xsize = eproc.e_xrssize =
-                                 eproc.e_xccount =  eproc.e_xswrss = 0;
-                       }
-                       if (error = copyout((caddr_t)&eproc, dp, 
+                       if (error = copyout((caddr_t)&eproc, &dp->kp_eproc, 
                            sizeof (eproc)))
                                return (error);
                            sizeof (eproc)))
                                return (error);
-                       dp += sizeof (eproc);
+                       dp++;
                        buflen -= sizeof (struct kinfo_proc);
                }
                needed += sizeof (struct kinfo_proc);
                        buflen -= sizeof (struct kinfo_proc);
                }
                needed += sizeof (struct kinfo_proc);
@@ -206,10 +185,119 @@ again:
                goto again;
        }
        if (where != NULL)
                goto again;
        }
        if (where != NULL)
-               *acopysize = dp - where;
+               *acopysize = (caddr_t)dp - where;
        else
                needed += KINFO_PROCSLOP;
        *aneeded = needed;
 
        return (0);
 }
        else
                needed += KINFO_PROCSLOP;
        *aneeded = needed;
 
        return (0);
 }
+
+/*
+ * Fill in an eproc structure for the specified process.
+ */
+void
+fill_eproc(p, ep)
+       register struct proc *p;
+       register struct eproc *ep;
+{
+       register struct tty *tp;
+
+       ep->e_paddr = p;
+       ep->e_sess = p->p_pgrp->pg_session;
+       ep->e_pcred = *p->p_cred;
+       ep->e_ucred = *p->p_ucred;
+       if (p->p_stat == SIDL || p->p_stat == SZOMB) {
+               ep->e_vm.vm_rssize = 0;
+               ep->e_vm.vm_tsize = 0;
+               ep->e_vm.vm_dsize = 0;
+               ep->e_vm.vm_ssize = 0;
+#ifndef sparc
+               /* ep->e_vm.vm_pmap = XXX; */
+#endif
+       } else {
+               register struct vmspace *vm = p->p_vmspace;
+
+               ep->e_vm.vm_rssize = vm->vm_rssize;
+               ep->e_vm.vm_tsize = vm->vm_tsize;
+               ep->e_vm.vm_dsize = vm->vm_dsize;
+               ep->e_vm.vm_ssize = vm->vm_ssize;
+#ifndef sparc
+               ep->e_vm.vm_pmap = vm->vm_pmap;
+#endif
+       }
+       if (p->p_pptr)
+               ep->e_ppid = p->p_pptr->p_pid;
+       else
+               ep->e_ppid = 0;
+       ep->e_pgid = p->p_pgrp->pg_id;
+       ep->e_jobc = p->p_pgrp->pg_jobc;
+       if ((p->p_flag&SCTTY) && 
+            (tp = ep->e_sess->s_ttyp)) {
+               ep->e_tdev = tp->t_dev;
+               ep->e_tpgid = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
+               ep->e_tsess = tp->t_session;
+       } else
+               ep->e_tdev = NODEV;
+       ep->e_flag = ep->e_sess->s_ttyvp ? EPROC_CTTY : 0;
+       if (SESS_LEADER(p))
+               ep->e_flag |= EPROC_SLEADER;
+       if (p->p_wmesg)
+               strncpy(ep->e_wmesg, p->p_wmesg, WMESGLEN);
+       ep->e_xsize = ep->e_xrssize = 0;
+       ep->e_xccount = ep->e_xswrss = 0;
+}
+
+/*
+ * Get file structures.
+ */
+kinfo_file(op, where, acopysize, arg, aneeded)
+       int op;
+       register char *where;
+       int *acopysize, arg, *aneeded;
+{
+       int buflen, needed, error;
+       struct file *fp;
+       char *start = where;
+
+       if (where == NULL) {
+               /*
+                * overestimate by 10 files
+                */
+               *aneeded = sizeof (filehead) + 
+                       (nfiles + 10) * sizeof (struct file);
+               return (0);
+       }
+       buflen = *acopysize;
+       needed = 0;
+
+       /*
+        * first copyout filehead
+        */
+       if (buflen > sizeof (filehead)) {
+               if (error = copyout((caddr_t)&filehead, where,
+                   sizeof (filehead)))
+                       return (error);
+               buflen -= sizeof (filehead);
+               where += sizeof (filehead);
+       }
+       needed += sizeof (filehead);
+
+       /*
+        * followed by an array of file structures
+        */
+       for (fp = filehead; fp != NULL; fp = fp->f_filef) {
+               if (buflen > sizeof (struct file)) {
+                       if (error = copyout((caddr_t)fp, where,
+                           sizeof (struct file)))
+                               return (error);
+                       buflen -= sizeof (struct file);
+                       where += sizeof (struct file);
+               }
+               needed += sizeof (struct file);
+       }
+       *acopysize = where - start;
+       *aneeded = needed;
+
+       return (0);
+}