unix.c revision 27753
1/*-
2 * Copyright (c) 1983, 1988, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)unix.c	8.1 (Berkeley) 6/6/93";
37#endif
38static const char rcsid[] =
39	"$Id$";
40#endif /* not lint */
41
42/*
43 * Display protocol blocks in the unix domain.
44 */
45#include <kvm.h>
46#include <sys/param.h>
47#include <sys/queue.h>
48#include <sys/protosw.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/mbuf.h>
52#include <sys/sysctl.h>
53#include <sys/un.h>
54#include <sys/unpcb.h>
55#define KERNEL
56struct uio;
57struct proc;
58#include <sys/file.h>
59
60#include <netinet/in.h>
61
62#include <stdio.h>
63#include <stdlib.h>
64#include "netstat.h"
65
66static	void unixdomainpr __P((struct socket *, caddr_t));
67
68static struct	file *file, *fileNFILE;
69static int	nfiles;
70extern	kvm_t *kvmd;
71
72void
73unixpr(off)
74	u_long	off;
75{
76	register struct file *fp;
77	struct socket sock, *so = &sock;
78	char *filebuf;
79	struct protosw *unixsw = (struct protosw *)off;
80
81	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
82	if (filebuf == 0) {
83		printf("Out of memory (file table).\n");
84		return;
85	}
86	file = (struct file *)(filebuf + sizeof(fp));
87	fileNFILE = file + nfiles;
88	for (fp = file; fp < fileNFILE; fp++) {
89		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
90			continue;
91		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
92			continue;
93		/* kludge */
94		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
95			if (so->so_pcb)
96				unixdomainpr(so, fp->f_data);
97	}
98}
99
100static	char *socktype[] =
101    { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
102
103static void
104unixdomainpr(so, soaddr)
105	register struct socket *so;
106	caddr_t soaddr;
107{
108	struct unpcb unpcb, *unp = &unpcb;
109	struct mbuf mbuf, *m;
110	struct sockaddr_un *sa = NULL;
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(
125"%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
126		    "Address", "Type", "Recv-Q", "Send-Q",
127		    "Inode", "Conn", "Refs", "Nextref");
128		first = 0;
129	}
130	printf("%8x %-6.6s %6ld %6ld %8x %8x %8x %8x",
131	    (int)soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
132	    (int)unp->unp_vnode, (int)unp->unp_conn,
133	    (int)unp->unp_refs, (int)unp->unp_nextref);
134	if (m)
135		printf(" %.*s",
136		    m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path)),
137		    sa->sun_path);
138	putchar('\n');
139}
140