BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / kern / kern_kinfo.c
index 8cee907..f97eff2 100644 (file)
@@ -2,37 +2,51 @@
  * 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 is only permitted until one year after the first shipment
- * of 4.4BSD by the Regents.  Otherwise, redistribution and use in source and
- * binary forms are permitted provided that: (1) source distributions retain
- * this entire copyright notice and comment, and (2) distributions including
- * binaries display the following acknowledgement:  This product includes
- * software developed by the University of California, Berkeley and its
- * contributors'' in the documentation or other materials provided with the
- * distribution and in all advertising materials mentioning features or use
- * of this software.  Neither the name of the University nor the names of
- * its contributors may 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.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *     @(#)kern_kinfo.c        7.11 (Berkeley) 6/28/90
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)kern_kinfo.c        7.17 (Berkeley) 6/26/91
  */
 
 #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(), kinfo_vnode();
+extern int kinfo_doproc(), kinfo_rtable(), kinfo_vnode(), kinfo_file();
 struct kinfo_lock kinfo_lock;
 
 /* ARGSUSED */
 struct kinfo_lock kinfo_lock;
 
 /* ARGSUSED */
@@ -47,11 +61,11 @@ getkerninfo(p, uap, retval)
        int *retval;
 {
 
        int *retval;
 {
 
-       int     bufsize,        /* max size of users buffer */
-               needed, locked, (*server)(), error = 0;
+       int bufsize;            /* max size of users buffer */
+       int needed, locked, (*server)(), error = 0;
 
 
-       if (error = copyin((caddr_t)uap->size,
-                               (caddr_t)&bufsize, sizeof (bufsize)))
+       if (error = copyin((caddr_t)uap->size, (caddr_t)&bufsize,
+           sizeof (bufsize)))
                goto done;
 
        switch (ki_type(uap->op)) {
                goto done;
 
        switch (ki_type(uap->op)) {
@@ -68,6 +82,10 @@ getkerninfo(p, uap, retval)
                server = kinfo_vnode;
                break;
 
                server = kinfo_vnode;
                break;
 
+       case KINFO_FILE:
+               server = kinfo_file;
+               break;
+
        default:
                error = EINVAL;
                goto done;
        default:
                error = EINVAL;
                goto done;
@@ -86,11 +104,6 @@ getkerninfo(p, uap, retval)
 
        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);
        if (server != kinfo_vnode)      /* XXX */
                vslock(uap->where, bufsize);
        locked = bufsize;
        if (server != kinfo_vnode)      /* XXX */
                vslock(uap->where, bufsize);
        locked = bufsize;
@@ -160,48 +173,20 @@ 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;
                }
                if (where != NULL && buflen >= sizeof (struct kinfo_proc)) {
                                continue;
                        break;
                }
                if (where != NULL && buflen >= sizeof (struct kinfo_proc)) {
-                       register struct text *txt;
-                       register struct tty *tp;
-
+                       fill_eproc(p, &eproc);
                        if (error = copyout((caddr_t)p, &dp->kp_proc, 
                            sizeof (struct proc)))
                                return (error);
                        if (error = copyout((caddr_t)p, &dp->kp_proc, 
                            sizeof (struct proc)))
                                return (error);
-                       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;
-                       if ((p->p_flag&SCTTY) && 
-                            (tp = eproc.e_sess->s_ttyp)) {
-                               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;
-                       eproc.e_flag = eproc.e_sess->s_ttyvp ? EPROC_CTTY : 0;
-                       if (SESS_LEADER(p))
-                               eproc.e_flag |= EPROC_SLEADER;
-                       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->kp_eproc, 
                            sizeof (eproc)))
                                return (error);
                        if (error = copyout((caddr_t)&eproc, &dp->kp_eproc, 
                            sizeof (eproc)))
                                return (error);
@@ -223,3 +208,93 @@ again:
 
        return (0);
 }
 
        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;
+       ep->e_vm = *p->p_vmspace;
+       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)
+       register char *where;
+       int *acopysize, *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);
+}