home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / netstat / unix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-01  |  4.5 KB  |  144 lines

  1. /*-
  2.  * Copyright (c) 1983, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)unix.c    5.11 (Berkeley) 7/1/91";
  36. #endif /* not lint */
  37.  
  38. /*
  39.  * Display protocol blocks in the unix domain.
  40.  */
  41. #include <sys/param.h>
  42. #include <sys/protosw.h>
  43. #include <sys/socket.h>
  44. #include <sys/socketvar.h>
  45. #include <sys/mbuf.h>
  46. #include <sys/un.h>
  47. #include <sys/unpcb.h>
  48. #define KERNEL
  49. struct uio;
  50. #include <sys/file.h>
  51. struct file *file, *fileNFILE;
  52. int nfile;
  53.  
  54. int    Aflag;
  55. extern    char *calloc();
  56.  
  57. unixpr(nfileaddr, fileaddr, unixsw)
  58.     off_t nfileaddr, fileaddr;
  59.     struct protosw *unixsw;
  60. {
  61.     register struct file *fp;
  62.     struct file *filep;
  63.     struct socket sock, *so = &sock;
  64.  
  65.     if (nfileaddr == 0 || fileaddr == 0) {
  66.         printf("nfile or file not in namelist.\n");
  67.         return;
  68.     }
  69.     if (kvm_read(nfileaddr, (char *)&nfile, sizeof (nfile)) !=
  70.                         sizeof (nfile)) {
  71.         printf("nfile: bad read.\n");
  72.         return;
  73.     }
  74.     if (kvm_read(fileaddr, (char *)&filep, sizeof (filep))
  75.                         != sizeof (filep)) {
  76.         printf("File table address, bad read.\n");
  77.         return;
  78.     }
  79.     file = (struct file *)calloc(nfile, sizeof (struct file));
  80.     if (file == (struct file *)0) {
  81.         printf("Out of memory (file table).\n");
  82.         return;
  83.     }
  84.     if (kvm_read((off_t)filep, (char *)file, nfile * sizeof (struct file))
  85.                     != nfile * sizeof (struct file)) {
  86.         printf("File table read error.\n");
  87.         return;
  88.     }
  89.     fileNFILE = file + nfile;
  90.     for (fp = file; fp < fileNFILE; fp++) {
  91.         if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
  92.             continue;
  93.         if (kvm_read((off_t)fp->f_data, (char *)so, sizeof (*so))
  94.                     != sizeof (*so))
  95.             continue;
  96.         /* kludge */
  97.         if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
  98.             if (so->so_pcb)
  99.                 unixdomainpr(so, fp->f_data);
  100.     }
  101.     free((char *)file);
  102. }
  103.  
  104. static    char *socktype[] =
  105.     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
  106.  
  107. unixdomainpr(so, soaddr)
  108.     register struct socket *so;
  109.     caddr_t soaddr;
  110. {
  111.     struct unpcb unpcb, *unp = &unpcb;
  112.     struct mbuf mbuf, *m;
  113.     struct sockaddr_un *sa;
  114.     static int first = 1;
  115.  
  116.     if (kvm_read((off_t)so->so_pcb, (char *)unp, sizeof (*unp))
  117.                 != sizeof (*unp))
  118.         return;
  119.     if (unp->unp_addr) {
  120.         m = &mbuf;
  121.         if (kvm_read((off_t)unp->unp_addr, (char *)m, sizeof (*m))
  122.                 != sizeof (*m))
  123.             m = (struct mbuf *)0;
  124.         sa = (struct sockaddr_un *)(m->m_dat);
  125.     } else
  126.         m = (struct mbuf *)0;
  127.     if (first) {
  128.         printf("Active UNIX domain sockets\n");
  129.         printf(
  130. "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
  131.             "Address", "Type", "Recv-Q", "Send-Q",
  132.             "Inode", "Conn", "Refs", "Nextref");
  133.         first = 0;
  134.     }
  135.     printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
  136.         soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
  137.         unp->unp_vnode, unp->unp_conn,
  138.         unp->unp_refs, unp->unp_nextref);
  139.     if (m)
  140.         printf(" %.*s", m->m_len - sizeof(sa->sun_family),
  141.             sa->sun_path);
  142.     putchar('\n');
  143. }
  144.