xref: /openbsd-src/usr.bin/netstat/unix.c (revision a4afd6dad3fba28f80e70208181c06c482259988)
1 /*	$OpenBSD: unix.c,v 1.2 1996/06/26 05:37:26 deraadt Exp $	*/
2 /*	$NetBSD: unix.c,v 1.13 1995/10/03 21:42:48 thorpej Exp $	*/
3 
4 /*-
5  * Copyright (c) 1983, 1988, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "from: @(#)unix.c	8.1 (Berkeley) 6/6/93";
40 #else
41 static char *rcsid = "$OpenBSD: unix.c,v 1.2 1996/06/26 05:37:26 deraadt 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 	register 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("Out of memory (file table).\n");
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 	register struct socket *so;
108 	caddr_t soaddr;
109 {
110 	struct unpcb unpcb, *unp = &unpcb;
111 	struct mbuf mbuf, *m;
112 	struct sockaddr_un *sa;
113 	static int first = 1;
114 
115 	if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
116 		return;
117 	if (unp->unp_addr) {
118 		m = &mbuf;
119 		if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
120 			m = (struct mbuf *)0;
121 		sa = (struct sockaddr_un *)(m->m_dat);
122 	} else
123 		m = (struct mbuf *)0;
124 	if (first) {
125 		printf("Active UNIX domain sockets\n");
126 		printf(
127 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
128 		    "Address", "Type", "Recv-Q", "Send-Q",
129 		    "Inode", "Conn", "Refs", "Nextref");
130 		first = 0;
131 	}
132 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
133 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
134 	    unp->unp_vnode, unp->unp_conn,
135 	    unp->unp_refs, unp->unp_nextref);
136 	if (m)
137 		printf(" %.*s",
138 		    m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path)),
139 		    sa->sun_path);
140 	putchar('\n');
141 }
142