xref: /openbsd/usr.bin/netstat/unix.c (revision fd84ef7e)
1 /*	$OpenBSD: unix.c,v 1.7 2001/11/19 19:02:15 mpech 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.7 2001/11/19 19:02:15 mpech 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 <limits.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <kvm.h>
67 #include "netstat.h"
68 
69 static	void unixdomainpr __P((struct socket *, caddr_t));
70 
71 static struct	file *file, *fileNFILE;
72 static int	nfiles;
73 extern	kvm_t *kvmd;
74 
75 void
76 unixpr(off)
77 	u_long	off;
78 {
79 	struct file *fp;
80 	struct socket sock, *so = &sock;
81 	char *filebuf;
82 	struct protosw *unixsw = (struct protosw *)off;
83 
84 	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
85 	if (filebuf == 0) {
86 		printf("Out of memory (file table).\n");
87 		return;
88 	}
89 	file = (struct file *)(filebuf + sizeof(fp));
90 	fileNFILE = file + nfiles;
91 	for (fp = file; fp < fileNFILE; fp++) {
92 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
93 			continue;
94 		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
95 			continue;
96 		/* kludge */
97 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
98 			if (so->so_pcb)
99 				unixdomainpr(so, fp->f_data);
100 	}
101 }
102 
103 static	char *socktype[] =
104     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
105 
106 static void
107 unixdomainpr(so, soaddr)
108 	struct socket *so;
109 	caddr_t soaddr;
110 {
111 	struct unpcb unpcb, *unp = &unpcb;
112 	struct mbuf mbuf, *m;
113 	struct sockaddr_un *sa = NULL;
114 	static int first = 1;
115 
116 	if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
117 		return;
118 	if (unp->unp_addr) {
119 		m = &mbuf;
120 		if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
121 			m = (struct mbuf *)0;
122 		sa = (struct sockaddr_un *)(m->m_dat);
123 	} else
124 		m = (struct mbuf *)0;
125 	if (first) {
126 		printf("Active UNIX domain sockets\n");
127 		printf(
128 "%-*.*s %-6.6s %-6.6s %-6.6s %*.*s %*.*s %*.*s %*.*s Addr\n",
129 		    PLEN, PLEN, "Address", "Type", "Recv-Q", "Send-Q",
130 		    PLEN, PLEN, "Inode", PLEN, PLEN, "Conn",
131 		    PLEN, PLEN, "Refs", PLEN, PLEN, "Nextref");
132 		first = 0;
133 	}
134 	printf("%*p %-6.6s %6ld %6ld %*p %*p %*p %*p",
135 	    PLEN, soaddr, socktype[so->so_type], so->so_rcv.sb_cc,
136 	    so->so_snd.sb_cc, PLEN, unp->unp_vnode, PLEN, unp->unp_conn,
137 	    PLEN, unp->unp_refs, PLEN, unp->unp_nextref);
138 	if (m)
139 		printf(" %.*s",
140 		    (int)(m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path))),
141 		    sa->sun_path);
142 	putchar('\n');
143 }
144