xref: /netbsd-src/usr.bin/netstat/unix.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: unix.c,v 1.21 2003/08/07 11:15:22 agc 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "from: @(#)unix.c	8.1 (Berkeley) 6/6/93";
36 #else
37 __RCSID("$NetBSD: unix.c,v 1.21 2003/08/07 11:15:22 agc Exp $");
38 #endif
39 #endif /* not lint */
40 
41 /*
42  * Display protocol blocks in the unix domain.
43  */
44 #include <sys/param.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/mbuf.h>
49 #include <sys/sysctl.h>
50 #include <sys/un.h>
51 #include <sys/unpcb.h>
52 #define _KERNEL
53 struct uio;
54 struct proc;
55 #include <sys/file.h>
56 
57 #include <netinet/in.h>
58 
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <kvm.h>
62 #include "netstat.h"
63 
64 static	void unixdomainpr __P((struct socket *, caddr_t));
65 
66 static struct	file *file, *fileNFILE;
67 static int	nfiles;
68 extern	kvm_t *kvmd;
69 
70 void
71 unixpr(off)
72 	u_long	off;
73 {
74 	struct file *fp;
75 	struct socket sock, *so = &sock;
76 	char *filebuf;
77 	struct protosw *unixsw = (struct protosw *)off;
78 
79 	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
80 	if (filebuf == 0) {
81 		printf("file table read error: %s", kvm_geterr(kvmd));
82 		return;
83 	}
84 	file = (struct file *)(filebuf + sizeof(fp));
85 	fileNFILE = file + nfiles;
86 	for (fp = file; fp < fileNFILE; fp++) {
87 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
88 			continue;
89 		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
90 			continue;
91 		/* kludge */
92 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
93 			if (so->so_pcb)
94 				unixdomainpr(so, fp->f_data);
95 	}
96 }
97 
98 static	char *socktype[] =
99     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
100 
101 static void
102 unixdomainpr(so, soaddr)
103 	struct socket *so;
104 	caddr_t soaddr;
105 {
106 	struct unpcb unp;
107 	struct sockaddr_un sun;
108 	static int first = 1;
109 
110 	if (kread((u_long)so->so_pcb, (char *)&unp, sizeof (unp)))
111 		return;
112 	if (unp.unp_addr)
113 		if (kread((u_long)unp.unp_addr, (char *)&sun, sizeof (sun)))
114 			unp.unp_addr = 0;
115 	if (first) {
116 		printf("Active UNIX domain sockets\n");
117 		printf(
118 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
119 		    "Address", "Type", "Recv-Q", "Send-Q",
120 		    "Inode", "Conn", "Refs", "Nextref");
121 		first = 0;
122 	}
123 	printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
124 	    (u_long) soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
125 	    (u_long) unp.unp_vnode, (u_long) unp.unp_conn,
126 	    (u_long) unp.unp_refs, (u_long) unp.unp_nextref);
127 	if (unp.unp_addr)
128 		printf(" %.*s",
129 		    (int) (sun.sun_len - (sizeof(sun) - sizeof(sun.sun_path))),
130 		    sun.sun_path);
131 	putchar('\n');
132 }
133