Annotation of src/usr.bin/netstat/unix.c, Revision 1.9
1.9 ! deraadt 1: /* $OpenBSD: unix.c,v 1.8 2002/02/16 21:27:50 millert Exp $ */
1.1 deraadt 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
1.9 ! deraadt 41: static char *rcsid = "$OpenBSD: unix.c,v 1.8 2002/02/16 21:27:50 millert Exp $";
1.1 deraadt 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:
1.5 millert 63: #include <limits.h>
1.1 deraadt 64: #include <stdio.h>
65: #include <stdlib.h>
66: #include <kvm.h>
67: #include "netstat.h"
68:
1.8 millert 69: static void unixdomainpr(struct socket *, caddr_t);
1.1 deraadt 70:
71: static struct file *file, *fileNFILE;
72: static int nfiles;
73: extern kvm_t *kvmd;
74:
75: void
1.9 ! deraadt 76: unixpr(u_long off)
1.1 deraadt 77: {
1.7 mpech 78: struct file *fp;
1.1 deraadt 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
1.9 ! deraadt 106: unixdomainpr(struct socket *so, caddr_t soaddr)
1.1 deraadt 107: {
108: struct unpcb unpcb, *unp = &unpcb;
109: struct mbuf mbuf, *m;
1.6 itojun 110: struct sockaddr_un *sa = NULL;
1.1 deraadt 111: static int first = 1;
112:
113: if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
114: return;
115: if (unp->unp_addr) {
116: m = &mbuf;
117: if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
118: m = (struct mbuf *)0;
119: sa = (struct sockaddr_un *)(m->m_dat);
120: } else
121: m = (struct mbuf *)0;
122: if (first) {
123: printf("Active UNIX domain sockets\n");
124: printf(
1.5 millert 125: "%-*.*s %-6.6s %-6.6s %-6.6s %*.*s %*.*s %*.*s %*.*s Addr\n",
126: PLEN, PLEN, "Address", "Type", "Recv-Q", "Send-Q",
127: PLEN, PLEN, "Inode", PLEN, PLEN, "Conn",
128: PLEN, PLEN, "Refs", PLEN, PLEN, "Nextref");
1.1 deraadt 129: first = 0;
130: }
1.5 millert 131: printf("%*p %-6.6s %6ld %6ld %*p %*p %*p %*p",
132: PLEN, soaddr, socktype[so->so_type], so->so_rcv.sb_cc,
133: so->so_snd.sb_cc, PLEN, unp->unp_vnode, PLEN, unp->unp_conn,
134: PLEN, unp->unp_refs, PLEN, unp->unp_nextref);
1.1 deraadt 135: if (m)
136: printf(" %.*s",
1.6 itojun 137: (int)(m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path))),
1.1 deraadt 138: sa->sun_path);
139: putchar('\n');
140: }