xref: /netbsd-src/usr.bin/netstat/unix.c (revision 1394f01b4a9e99092957ca5d824d67219565d9b5)
1 /*	$NetBSD: unix.c,v 1.16 1997/04/03 04:46:54 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1983, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "from: @(#)unix.c	8.1 (Berkeley) 6/6/93";
39 #else
40 static char *rcsid = "$NetBSD: unix.c,v 1.16 1997/04/03 04:46:54 christos Exp $";
41 #endif
42 #endif /* not lint */
43 
44 /*
45  * Display protocol blocks in the unix domain.
46  */
47 #include <sys/param.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 <kvm.h>
65 #include "netstat.h"
66 
67 static	void unixdomainpr __P((struct socket *, caddr_t));
68 
69 static struct	file *file, *fileNFILE;
70 static int	nfiles;
71 extern	kvm_t *kvmd;
72 
73 void
74 unixpr(off)
75 	u_long	off;
76 {
77 	register struct file *fp;
78 	struct socket sock, *so = &sock;
79 	char *filebuf;
80 	struct protosw *unixsw = (struct protosw *)off;
81 
82 	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
83 	if (filebuf == 0) {
84 		printf("Out of memory (file table).\n");
85 		return;
86 	}
87 	file = (struct file *)(filebuf + sizeof(fp));
88 	fileNFILE = file + nfiles;
89 	for (fp = file; fp < fileNFILE; fp++) {
90 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
91 			continue;
92 		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
93 			continue;
94 		/* kludge */
95 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
96 			if (so->so_pcb)
97 				unixdomainpr(so, fp->f_data);
98 	}
99 }
100 
101 static	char *socktype[] =
102     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
103 
104 static void
105 unixdomainpr(so, soaddr)
106 	register struct socket *so;
107 	caddr_t soaddr;
108 {
109 	struct unpcb unp;
110 	struct sockaddr_un sun;
111 	static int first = 1;
112 
113 	if (kread((u_long)so->so_pcb, (char *)&unp, sizeof (unp)))
114 		return;
115 	if (unp.unp_addr)
116 		if (kread((u_long)unp.unp_addr, (char *)&sun, sizeof (sun)))
117 			unp.unp_addr = 0;
118 	if (first) {
119 		printf("Active UNIX domain sockets\n");
120 		printf(
121 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
122 		    "Address", "Type", "Recv-Q", "Send-Q",
123 		    "Inode", "Conn", "Refs", "Nextref");
124 		first = 0;
125 	}
126 	printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
127 	    (u_long) soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
128 	    (u_long) unp.unp_vnode, (u_long) unp.unp_conn,
129 	    (u_long) unp.unp_refs, (u_long) unp.unp_nextref);
130 	if (unp.unp_addr)
131 		printf(" %.*s",
132 		    (int) (sun.sun_len - (sizeof(sun) - sizeof(sun.sun_path))),
133 		    sun.sun_path);
134 	putchar('\n');
135 }
136