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