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