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