xref: /386bsd/usr/src/usr.bin/netstat/unix.c (revision a2142627)
1 /*-
2  * Copyright (c) 1983, 1988 Regents of the University of California.
3  * 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	5.11 (Berkeley) 7/1/91"; */
36 static char sccsid[] = "$Id: unix.c,v 1.3 93/03/15 19:31:53 bill Exp $";
37 #endif /* not lint */
38 
39 /*
40  * Display protocol blocks in the unix domain.
41  */
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/un.h>
48 #include <sys/unpcb.h>
49 #define KERNEL
50 struct uio;
51 struct proc;
52 #include <sys/file.h>
53 
54 int	Aflag;
55 
unixpr(fileheadaddr,unixsw)56 unixpr(fileheadaddr, unixsw)
57 	off_t fileheadaddr;
58 	struct protosw *unixsw;
59 {
60 	struct file file, *filep;
61 	struct socket sock, *so = &sock;
62 
63 	if (fileheadaddr == 0) {
64 		printf("filehead not in namelist.\n");
65 		return;
66 	}
67 	if (kvm_read(fileheadaddr, (char *)&filep, sizeof (filep))
68 						!= sizeof (filep)) {
69 		printf("Open file list head address, bad read.\n");
70 		return;
71 	}
72 	while (filep) {
73 		if (kvm_read((off_t)filep, (char *)&file, sizeof (struct file))
74 					!= sizeof (struct file)) {
75 			printf("File table read error.\n");
76 			return;
77 		}
78 		filep = file.f_filef;
79 		if (file.f_type != DTYPE_SOCKET)
80 			continue;
81 		if (kvm_read((off_t)file.f_data, (char *)so, sizeof (*so))
82 					!= sizeof (*so))
83 			continue;
84 		/* kludge */
85 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
86 			if (so->so_pcb)
87 				unixdomainpr(so, file.f_data);
88 	}
89 }
90 
91 static	char *socktype[] =
92     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
93 
unixdomainpr(so,soaddr)94 unixdomainpr(so, soaddr)
95 	register struct socket *so;
96 	caddr_t soaddr;
97 {
98 	struct unpcb unpcb, *unp = &unpcb;
99 	struct mbuf mbuf, *m;
100 	struct sockaddr_un *sa;
101 	static int first = 1;
102 
103 	if (kvm_read((off_t)so->so_pcb, (char *)unp, sizeof (*unp))
104 				!= sizeof (*unp))
105 		return;
106 	if (unp->unp_addr) {
107 		m = &mbuf;
108 		if (kvm_read((off_t)unp->unp_addr, (char *)m, sizeof (*m))
109 				!= sizeof (*m))
110 			m = (struct mbuf *)0;
111 		sa = (struct sockaddr_un *)(m->m_dat);
112 	} else
113 		m = (struct mbuf *)0;
114 	if (first) {
115 		printf("Active UNIX domain sockets\n");
116 		printf(
117 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
118 		    "Address", "Type", "Recv-Q", "Send-Q",
119 		    "Inode", "Conn", "Refs", "Nextref");
120 		first = 0;
121 	}
122 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
123 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
124 	    unp->unp_vnode, unp->unp_conn,
125 	    unp->unp_refs, unp->unp_nextref);
126 	if (m)
127 		printf(" %.*s", m->m_len - sizeof(sa->sun_family),
128 		    sa->sun_path);
129 	putchar('\n');
130 }
131