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