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