xref: /csrg-svn/usr.bin/netstat/unix.c (revision 53698)
143249Skarels /*-
234902Sbostic  * Copyright (c) 1983, 1988 Regents of the University of California.
333451Skarels  * All rights reserved.
433451Skarels  *
543249Skarels  * %sccs.include.redist.c%
621991Sdist  */
721991Sdist 
816560Ssam #ifndef lint
9*53698Ssklower static char sccsid[] = "@(#)unix.c	5.12 (Berkeley) 05/27/92";
1034902Sbostic #endif /* not lint */
1116560Ssam 
1216560Ssam /*
1316560Ssam  * Display protocol blocks in the unix domain.
1416560Ssam  */
15*53698Ssklower #include <kvm.h>
1616560Ssam #include <sys/param.h>
1716560Ssam #include <sys/protosw.h>
1816560Ssam #include <sys/socket.h>
1916560Ssam #include <sys/socketvar.h>
2016560Ssam #include <sys/mbuf.h>
21*53698Ssklower #include <sys/kinfo.h>
2216560Ssam #include <sys/un.h>
2316560Ssam #include <sys/unpcb.h>
2448790Sbostic #define KERNEL
2548790Sbostic struct uio;
26*53698Ssklower struct proc;
2716560Ssam #include <sys/file.h>
28*53698Ssklower #include <stdlib.h>
29*53698Ssklower #include "netstat.h"
30*53698Ssklower 
31*53698Ssklower static void unixdomainpr __P((struct socket *, caddr_t));
32*53698Ssklower 
3350353Skarels struct file *file, *fileNFILE;
34*53698Ssklower int nfiles;
35*53698Ssklower extern	kvm_t *kvmd;
3616560Ssam 
37*53698Ssklower void
38*53698Ssklower unixpr(unixsw)
3916560Ssam 	struct protosw *unixsw;
4016560Ssam {
4116560Ssam 	register struct file *fp;
4216560Ssam 	struct socket sock, *so = &sock;
43*53698Ssklower 	char *filebuf;
4416560Ssam 
45*53698Ssklower 	filebuf = (char *)kvm_getfiles(kvmd, KINFO_FILE, 0, &nfiles);
46*53698Ssklower 	if (filebuf == 0) {
4716560Ssam 		printf("Out of memory (file table).\n");
4816560Ssam 		return;
4916560Ssam 	}
50*53698Ssklower 	file = (struct file *)(filebuf + sizeof(fp));
51*53698Ssklower 	fileNFILE = file + nfiles;
5216560Ssam 	for (fp = file; fp < fileNFILE; fp++) {
5316560Ssam 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
5416560Ssam 			continue;
55*53698Ssklower 		if (kread((off_t)fp->f_data, (char *)so, sizeof (*so)))
5616560Ssam 			continue;
5716560Ssam 		/* kludge */
5816560Ssam 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
5916560Ssam 			if (so->so_pcb)
6016560Ssam 				unixdomainpr(so, fp->f_data);
6116560Ssam 	}
6216560Ssam }
6316560Ssam 
6416560Ssam static	char *socktype[] =
6516560Ssam     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
6616560Ssam 
67*53698Ssklower static void
6816560Ssam unixdomainpr(so, soaddr)
6916560Ssam 	register struct socket *so;
7016560Ssam 	caddr_t soaddr;
7116560Ssam {
7216560Ssam 	struct unpcb unpcb, *unp = &unpcb;
7316560Ssam 	struct mbuf mbuf, *m;
7427887Skarels 	struct sockaddr_un *sa;
7516560Ssam 	static int first = 1;
7616560Ssam 
77*53698Ssklower 	if (kread((off_t)so->so_pcb, (char *)unp, sizeof (*unp)))
7816560Ssam 		return;
7926022Smckusick 	if (unp->unp_addr) {
8016560Ssam 		m = &mbuf;
81*53698Ssklower 		if (kread((off_t)unp->unp_addr, (char *)m, sizeof (*m)))
8216560Ssam 			m = (struct mbuf *)0;
8339215Skarels 		sa = (struct sockaddr_un *)(m->m_dat);
8416560Ssam 	} else
8516560Ssam 		m = (struct mbuf *)0;
8616560Ssam 	if (first) {
8727887Skarels 		printf("Active UNIX domain sockets\n");
8816560Ssam 		printf(
8927887Skarels "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
9016560Ssam 		    "Address", "Type", "Recv-Q", "Send-Q",
9116560Ssam 		    "Inode", "Conn", "Refs", "Nextref");
9216560Ssam 		first = 0;
9316560Ssam 	}
9416560Ssam 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
9516560Ssam 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
9643249Skarels 	    unp->unp_vnode, unp->unp_conn,
9716560Ssam 	    unp->unp_refs, unp->unp_nextref);
9816560Ssam 	if (m)
9927887Skarels 		printf(" %.*s", m->m_len - sizeof(sa->sun_family),
10027887Skarels 		    sa->sun_path);
10116560Ssam 	putchar('\n');
10216560Ssam }
103