not every value return in an nlist structure was being
[unix-history] / usr / src / usr.bin / netstat / unix.c
CommitLineData
64addc1e 1/*-
b36fc510 2 * Copyright (c) 1983, 1988 Regents of the University of California.
ee3f90a5
MK
3 * All rights reserved.
4 *
64addc1e 5 * %sccs.include.redist.c%
5ff67f98
DF
6 */
7
0b1fe039 8#ifndef lint
d1cfb820 9static char sccsid[] = "@(#)unix.c 5.14 (Berkeley) %G%";
b36fc510 10#endif /* not lint */
0b1fe039
SL
11
12/*
13 * Display protocol blocks in the unix domain.
14 */
6e549c8f 15#include <kvm.h>
0b1fe039
SL
16#include <sys/param.h>
17#include <sys/protosw.h>
18#include <sys/socket.h>
19#include <sys/socketvar.h>
20#include <sys/mbuf.h>
6e549c8f 21#include <sys/kinfo.h>
0b1fe039
SL
22#include <sys/un.h>
23#include <sys/unpcb.h>
26596563
KB
24#define KERNEL
25struct uio;
6e549c8f 26struct proc;
0b1fe039 27#include <sys/file.h>
27788811
KS
28
29#include <netinet/in.h>
30
31#include <stdio.h>
6e549c8f
KS
32#include <stdlib.h>
33#include "netstat.h"
34
27788811 35static void unixdomainpr __P((struct socket *, caddr_t));
0b1fe039 36
27788811
KS
37struct file *file, *fileNFILE;
38int nfiles;
6e549c8f 39extern kvm_t *kvmd;
0b1fe039 40
6e549c8f 41void
d1cfb820
KS
42unixpr(off)
43 u_long off;
0b1fe039
SL
44{
45 register struct file *fp;
0b1fe039 46 struct socket sock, *so = &sock;
6e549c8f 47 char *filebuf;
d1cfb820 48 struct protosw *unixsw = (struct protosw *)off;
0b1fe039 49
6e549c8f
KS
50 filebuf = (char *)kvm_getfiles(kvmd, KINFO_FILE, 0, &nfiles);
51 if (filebuf == 0) {
0b1fe039
SL
52 printf("Out of memory (file table).\n");
53 return;
54 }
6e549c8f
KS
55 file = (struct file *)(filebuf + sizeof(fp));
56 fileNFILE = file + nfiles;
0b1fe039
SL
57 for (fp = file; fp < fileNFILE; fp++) {
58 if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
59 continue;
d1cfb820 60 if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
0b1fe039
SL
61 continue;
62 /* kludge */
63 if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
64 if (so->so_pcb)
65 unixdomainpr(so, fp->f_data);
66 }
0b1fe039
SL
67}
68
69static char *socktype[] =
70 { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
71
6e549c8f 72static void
0b1fe039
SL
73unixdomainpr(so, soaddr)
74 register struct socket *so;
75 caddr_t soaddr;
76{
77 struct unpcb unpcb, *unp = &unpcb;
78 struct mbuf mbuf, *m;
2a893f10 79 struct sockaddr_un *sa;
0b1fe039
SL
80 static int first = 1;
81
d1cfb820 82 if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
0b1fe039 83 return;
57140966 84 if (unp->unp_addr) {
0b1fe039 85 m = &mbuf;
d1cfb820 86 if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
0b1fe039 87 m = (struct mbuf *)0;
39173c8c 88 sa = (struct sockaddr_un *)(m->m_dat);
0b1fe039
SL
89 } else
90 m = (struct mbuf *)0;
91 if (first) {
2a893f10 92 printf("Active UNIX domain sockets\n");
0b1fe039 93 printf(
2a893f10 94"%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
0b1fe039
SL
95 "Address", "Type", "Recv-Q", "Send-Q",
96 "Inode", "Conn", "Refs", "Nextref");
97 first = 0;
98 }
99 printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
100 soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
64addc1e 101 unp->unp_vnode, unp->unp_conn,
0b1fe039
SL
102 unp->unp_refs, unp->unp_nextref);
103 if (m)
27788811 104 printf(" %.*s", m->m_len - (int)sizeof(sa->sun_family),
2a893f10 105 sa->sun_path);
0b1fe039
SL
106 putchar('\n');
107}