xref: /original-bsd/usr.bin/netstat/unix.c (revision 3f14a87d)
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.16 (Berkeley) 04/01/93";
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/sysctl.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 
29 #include <netinet/in.h>
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "netstat.h"
34 
35 static	void unixdomainpr __P((struct socket *, caddr_t));
36 
37 static struct	file *file, *fileNFILE;
38 static int	nfiles;
39 extern	kvm_t *kvmd;
40 
41 void
42 unixpr(off)
43 	u_long	off;
44 {
45 	register struct file *fp;
46 	struct socket sock, *so = &sock;
47 	char *filebuf;
48 	struct protosw *unixsw = (struct protosw *)off;
49 
50 	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
51 	if (filebuf == 0) {
52 		printf("Out of memory (file table).\n");
53 		return;
54 	}
55 	file = (struct file *)(filebuf + sizeof(fp));
56 	fileNFILE = file + nfiles;
57 	for (fp = file; fp < fileNFILE; fp++) {
58 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
59 			continue;
60 		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
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 	}
67 }
68 
69 static	char *socktype[] =
70     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
71 
72 static void
73 unixdomainpr(so, soaddr)
74 	register struct socket *so;
75 	caddr_t soaddr;
76 {
77 	struct unpcb unpcb, *unp = &unpcb;
78 	struct mbuf mbuf, *m;
79 	struct sockaddr_un *sa;
80 	static int first = 1;
81 
82 	if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
83 		return;
84 	if (unp->unp_addr) {
85 		m = &mbuf;
86 		if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
87 			m = (struct mbuf *)0;
88 		sa = (struct sockaddr_un *)(m->m_dat);
89 	} else
90 		m = (struct mbuf *)0;
91 	if (first) {
92 		printf("Active UNIX domain sockets\n");
93 		printf(
94 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
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,
101 	    unp->unp_vnode, unp->unp_conn,
102 	    unp->unp_refs, unp->unp_nextref);
103 	if (m)
104 		printf(" %.*s",
105 		    m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path)),
106 		    sa->sun_path);
107 	putchar('\n');
108 }
109