xref: /netbsd/usr.bin/netstat/unix.c (revision bf9ec67e)
1 /*	$NetBSD: unix.c,v 1.18 1999/08/19 06:13:09 cgd 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "from: @(#)unix.c	8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: unix.c,v 1.18 1999/08/19 06:13:09 cgd Exp $");
42 #endif
43 #endif /* not lint */
44 
45 /*
46  * Display protocol blocks in the unix domain.
47  */
48 #include <sys/param.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/mbuf.h>
53 #include <sys/sysctl.h>
54 #include <sys/un.h>
55 #include <sys/unpcb.h>
56 #define _KERNEL
57 struct uio;
58 struct proc;
59 #include <sys/file.h>
60 
61 #include <netinet/in.h>
62 
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <kvm.h>
66 #include "netstat.h"
67 
68 static	void unixdomainpr __P((struct socket *, caddr_t));
69 
70 static struct	file *file, *fileNFILE;
71 static int	nfiles;
72 extern	kvm_t *kvmd;
73 
74 void
75 unixpr(off)
76 	u_long	off;
77 {
78 	struct file *fp;
79 	struct socket sock, *so = &sock;
80 	char *filebuf;
81 	struct protosw *unixsw = (struct protosw *)off;
82 
83 	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
84 	if (filebuf == 0) {
85 		printf("file table read error: %s", kvm_geterr(kvmd));
86 		return;
87 	}
88 	file = (struct file *)(filebuf + sizeof(fp));
89 	fileNFILE = file + nfiles;
90 	for (fp = file; fp < fileNFILE; fp++) {
91 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
92 			continue;
93 		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
94 			continue;
95 		/* kludge */
96 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
97 			if (so->so_pcb)
98 				unixdomainpr(so, fp->f_data);
99 	}
100 }
101 
102 static	char *socktype[] =
103     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
104 
105 static void
106 unixdomainpr(so, soaddr)
107 	struct socket *so;
108 	caddr_t soaddr;
109 {
110 	struct unpcb unp;
111 	struct sockaddr_un sun;
112 	static int first = 1;
113 
114 	if (kread((u_long)so->so_pcb, (char *)&unp, sizeof (unp)))
115 		return;
116 	if (unp.unp_addr)
117 		if (kread((u_long)unp.unp_addr, (char *)&sun, sizeof (sun)))
118 			unp.unp_addr = 0;
119 	if (first) {
120 		printf("Active UNIX domain sockets\n");
121 		printf(
122 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
123 		    "Address", "Type", "Recv-Q", "Send-Q",
124 		    "Inode", "Conn", "Refs", "Nextref");
125 		first = 0;
126 	}
127 	printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
128 	    (u_long) soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
129 	    (u_long) unp.unp_vnode, (u_long) unp.unp_conn,
130 	    (u_long) unp.unp_refs, (u_long) unp.unp_nextref);
131 	if (unp.unp_addr)
132 		printf(" %.*s",
133 		    (int) (sun.sun_len - (sizeof(sun) - sizeof(sun.sun_path))),
134 		    sun.sun_path);
135 	putchar('\n');
136 }
137