xref: /freebsd/usr.bin/netstat/unix.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
1 /*-
2  * Copyright (c) 1983, 1988, 1993
3  *	The Regents of the University of California.  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	8.1 (Berkeley) 6/6/93";
36 #endif /* not lint */
37 
38 /*
39  * Display protocol blocks in the unix domain.
40  */
41 #include <kvm.h>
42 #include <sys/param.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/mbuf.h>
47 #include <sys/sysctl.h>
48 #include <sys/un.h>
49 #include <sys/unpcb.h>
50 #define KERNEL
51 struct uio;
52 struct proc;
53 #include <sys/file.h>
54 
55 #include <netinet/in.h>
56 
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include "netstat.h"
60 
61 static	void unixdomainpr __P((struct socket *, caddr_t));
62 
63 static struct	file *file, *fileNFILE;
64 static int	nfiles;
65 extern	kvm_t *kvmd;
66 
67 void
68 unixpr(off)
69 	u_long	off;
70 {
71 	register struct file *fp;
72 	struct socket sock, *so = &sock;
73 	char *filebuf;
74 	struct protosw *unixsw = (struct protosw *)off;
75 
76 	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
77 	if (filebuf == 0) {
78 		printf("Out of memory (file table).\n");
79 		return;
80 	}
81 	file = (struct file *)(filebuf + sizeof(fp));
82 	fileNFILE = file + nfiles;
83 	for (fp = file; fp < fileNFILE; fp++) {
84 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
85 			continue;
86 		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
87 			continue;
88 		/* kludge */
89 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
90 			if (so->so_pcb)
91 				unixdomainpr(so, fp->f_data);
92 	}
93 }
94 
95 static	char *socktype[] =
96     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
97 
98 static void
99 unixdomainpr(so, soaddr)
100 	register struct socket *so;
101 	caddr_t soaddr;
102 {
103 	struct unpcb unpcb, *unp = &unpcb;
104 	struct mbuf mbuf, *m;
105 	struct sockaddr_un *sa;
106 	static int first = 1;
107 
108 	if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
109 		return;
110 	if (unp->unp_addr) {
111 		m = &mbuf;
112 		if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
113 			m = (struct mbuf *)0;
114 		sa = (struct sockaddr_un *)(m->m_dat);
115 	} else
116 		m = (struct mbuf *)0;
117 	if (first) {
118 		printf("Active UNIX domain sockets\n");
119 		printf(
120 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
121 		    "Address", "Type", "Recv-Q", "Send-Q",
122 		    "Inode", "Conn", "Refs", "Nextref");
123 		first = 0;
124 	}
125 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
126 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
127 	    unp->unp_vnode, unp->unp_conn,
128 	    unp->unp_refs, unp->unp_nextref);
129 	if (m)
130 		printf(" %.*s",
131 		    m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path)),
132 		    sa->sun_path);
133 	putchar('\n');
134 }
135