xref: /openbsd-src/usr.bin/netstat/unix.c (revision 33b4f39fbeffad07bc3206f173cff9f3c9901cd1)
1 /*	$OpenBSD: unix.c,v 1.11 2004/03/13 22:02:13 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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "from: @(#)unix.c	8.1 (Berkeley) 6/6/93";
36 #else
37 static char *rcsid = "$OpenBSD: unix.c,v 1.11 2004/03/13 22:02:13 deraadt 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 <limits.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <kvm.h>
63 #include "netstat.h"
64 
65 static	void unixdomainpr(struct socket *, caddr_t);
66 
67 static struct	file *file, *fileNFILE;
68 static int	nfiles;
69 extern	kvm_t *kvmd;
70 
71 void
72 unixpr(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 == NULL) {
81 		printf("Out of memory (file table).\n");
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(struct socket *so, caddr_t soaddr)
103 {
104 	struct unpcb unpcb, *unp = &unpcb;
105 	struct mbuf mbuf, *m;
106 	struct sockaddr_un *sa = NULL;
107 	static int first = 1;
108 
109 	if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
110 		return;
111 	if (unp->unp_addr) {
112 		m = &mbuf;
113 		if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
114 			m = NULL;
115 		sa = (struct sockaddr_un *)(m->m_dat);
116 	} else
117 		m = NULL;
118 	if (first) {
119 		printf("Active UNIX domain sockets\n");
120 		printf("%-*.*s %-6.6s %-6.6s %-6.6s %*.*s %*.*s %*.*s %*.*s Addr\n",
121 		    PLEN, PLEN, "Address", "Type", "Recv-Q", "Send-Q",
122 		    PLEN, PLEN, "Inode", PLEN, PLEN, "Conn",
123 		    PLEN, PLEN, "Refs", PLEN, PLEN, "Nextref");
124 		first = 0;
125 	}
126 	printf("%*p %-6.6s %6ld %6ld %*p %*p %*p %*p",
127 	    PLEN, soaddr, socktype[so->so_type], so->so_rcv.sb_cc,
128 	    so->so_snd.sb_cc, PLEN, unp->unp_vnode, PLEN, unp->unp_conn,
129 	    PLEN, unp->unp_refs, PLEN, unp->unp_nextref);
130 	if (m)
131 		printf(" %.*s",
132 		    (int)(m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path))),
133 		    sa->sun_path);
134 	putchar('\n');
135 }
136