xref: /netbsd-src/usr.bin/netstat/unix.c (revision 811e6386f8c5e4a3521c7003da29ec8673e344fa)
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 #endif /* not lint */
37 
38 /*
39  * Display protocol blocks in the unix domain.
40  */
41 #include <sys/param.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/mbuf.h>
46 #include <sys/un.h>
47 #include <sys/unpcb.h>
48 #include <sys/time.h>
49 #include <sys/proc.h>
50 #define KERNEL
51 struct uio;
52 #include <sys/file.h>
53 struct file *file, *fileNFILE;
54 int nfiles;
55 
56 int	Aflag;
57 extern	char *calloc();
58 
59 unixpr(fileheadaddr, nfilesaddr, unixsw)
60 	off_t fileheadaddr, nfilesaddr;
61 	struct protosw *unixsw;
62 {
63 	register struct file *fp, *lfp;
64 	struct file *filep;
65 	struct socket sock, *so = &sock;
66 	int i;
67 
68 	if (fileheadaddr == 0 || nfilesaddr == 0) {
69 		printf("filehead or nfiles not in namelist.\n");
70 		return;
71 	}
72 	if (kvm_read(nfilesaddr, (char *)&nfiles, sizeof (nfiles)) !=
73 						sizeof (nfiles)) {
74 		printf("nfiles: bad read.\n");
75 		return;
76 	}
77 	if (kvm_read(fileheadaddr, (char *)&filep, sizeof (filep))
78 						!= sizeof (filep)) {
79 		printf("File table address, bad read.\n");
80 		return;
81 	}
82 	file = (struct file *)calloc(nfiles, sizeof (struct file));
83 	if (file == (struct file *)0) {
84 		printf("Out of memory (file table).\n");
85 		return;
86 	}
87 	for (lfp = file, i = nfiles;
88 		filep != NULL && i-- > 0;
89 		filep = lfp->f_filef, lfp++)
90 	{
91 		if(kvm_read((off_t)filep, (char *)lfp, sizeof (struct file))
92 						!= sizeof(struct file)) {
93 			printf("File table read error.\n");
94 			return;
95 		}
96 	}
97 	fileNFILE = file + nfiles;
98 	for (fp = file; fp < fileNFILE; fp++) {
99 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
100 			continue;
101 		if (kvm_read((off_t)fp->f_data, (char *)so, sizeof (*so))
102 					!= sizeof (*so))
103 			continue;
104 		/* kludge */
105 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
106 			if (so->so_pcb)
107 				unixdomainpr(so, fp->f_data);
108 	}
109 	free((char *)file);
110 }
111 
112 static	char *socktype[] =
113     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
114 
115 unixdomainpr(so, soaddr)
116 	register struct socket *so;
117 	caddr_t soaddr;
118 {
119 	struct unpcb unpcb, *unp = &unpcb;
120 	struct mbuf mbuf, *m;
121 	struct sockaddr_un *sa;
122 	static int first = 1;
123 
124 	if (kvm_read((off_t)so->so_pcb, (char *)unp, sizeof (*unp))
125 				!= sizeof (*unp))
126 		return;
127 	if (unp->unp_addr) {
128 		m = &mbuf;
129 		if (kvm_read((off_t)unp->unp_addr, (char *)m, sizeof (*m))
130 				!= sizeof (*m))
131 			m = (struct mbuf *)0;
132 		sa = (struct sockaddr_un *)(m->m_dat);
133 	} else
134 		m = (struct mbuf *)0;
135 	if (first) {
136 		printf("Active UNIX domain sockets\n");
137 		printf(
138 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
139 		    "Address", "Type", "Recv-Q", "Send-Q",
140 		    "Inode", "Conn", "Refs", "Nextref");
141 		first = 0;
142 	}
143 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
144 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
145 	    unp->unp_vnode, unp->unp_conn,
146 	    unp->unp_refs, unp->unp_nextref);
147 	if (m)
148 		printf(" %.*s", m->m_len - sizeof(sa->sun_family),
149 		    sa->sun_path);
150 	putchar('\n');
151 }
152