xref: /csrg-svn/usr.bin/netstat/unix.c (revision 54721)
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*54721Ssklower static char sccsid[] = "@(#)unix.c	5.13 (Berkeley) 07/06/92";
1034902Sbostic #endif /* not lint */
1116560Ssam 
1216560Ssam /*
1316560Ssam  * Display protocol blocks in the unix domain.
1416560Ssam  */
1553698Ssklower #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>
2153698Ssklower #include <sys/kinfo.h>
2216560Ssam #include <sys/un.h>
2316560Ssam #include <sys/unpcb.h>
2448790Sbostic #define KERNEL
2548790Sbostic struct uio;
2653698Ssklower struct proc;
2716560Ssam #include <sys/file.h>
28*54721Ssklower 
29*54721Ssklower #include <netinet/in.h>
30*54721Ssklower 
31*54721Ssklower #include <stdio.h>
3253698Ssklower #include <stdlib.h>
3353698Ssklower #include "netstat.h"
3453698Ssklower 
35*54721Ssklower static	void unixdomainpr __P((struct socket *, caddr_t));
3653698Ssklower 
37*54721Ssklower struct	file *file, *fileNFILE;
38*54721Ssklower int	nfiles;
3953698Ssklower extern	kvm_t *kvmd;
4016560Ssam 
4153698Ssklower void
4253698Ssklower unixpr(unixsw)
4316560Ssam 	struct protosw *unixsw;
4416560Ssam {
4516560Ssam 	register struct file *fp;
4616560Ssam 	struct socket sock, *so = &sock;
4753698Ssklower 	char *filebuf;
4816560Ssam 
4953698Ssklower 	filebuf = (char *)kvm_getfiles(kvmd, KINFO_FILE, 0, &nfiles);
5053698Ssklower 	if (filebuf == 0) {
5116560Ssam 		printf("Out of memory (file table).\n");
5216560Ssam 		return;
5316560Ssam 	}
5453698Ssklower 	file = (struct file *)(filebuf + sizeof(fp));
5553698Ssklower 	fileNFILE = file + nfiles;
5616560Ssam 	for (fp = file; fp < fileNFILE; fp++) {
5716560Ssam 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
5816560Ssam 			continue;
5953698Ssklower 		if (kread((off_t)fp->f_data, (char *)so, sizeof (*so)))
6016560Ssam 			continue;
6116560Ssam 		/* kludge */
6216560Ssam 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
6316560Ssam 			if (so->so_pcb)
6416560Ssam 				unixdomainpr(so, fp->f_data);
6516560Ssam 	}
6616560Ssam }
6716560Ssam 
6816560Ssam static	char *socktype[] =
6916560Ssam     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
7016560Ssam 
7153698Ssklower static void
7216560Ssam unixdomainpr(so, soaddr)
7316560Ssam 	register struct socket *so;
7416560Ssam 	caddr_t soaddr;
7516560Ssam {
7616560Ssam 	struct unpcb unpcb, *unp = &unpcb;
7716560Ssam 	struct mbuf mbuf, *m;
7827887Skarels 	struct sockaddr_un *sa;
7916560Ssam 	static int first = 1;
8016560Ssam 
8153698Ssklower 	if (kread((off_t)so->so_pcb, (char *)unp, sizeof (*unp)))
8216560Ssam 		return;
8326022Smckusick 	if (unp->unp_addr) {
8416560Ssam 		m = &mbuf;
8553698Ssklower 		if (kread((off_t)unp->unp_addr, (char *)m, sizeof (*m)))
8616560Ssam 			m = (struct mbuf *)0;
8739215Skarels 		sa = (struct sockaddr_un *)(m->m_dat);
8816560Ssam 	} else
8916560Ssam 		m = (struct mbuf *)0;
9016560Ssam 	if (first) {
9127887Skarels 		printf("Active UNIX domain sockets\n");
9216560Ssam 		printf(
9327887Skarels "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
9416560Ssam 		    "Address", "Type", "Recv-Q", "Send-Q",
9516560Ssam 		    "Inode", "Conn", "Refs", "Nextref");
9616560Ssam 		first = 0;
9716560Ssam 	}
9816560Ssam 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
9916560Ssam 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
10043249Skarels 	    unp->unp_vnode, unp->unp_conn,
10116560Ssam 	    unp->unp_refs, unp->unp_nextref);
10216560Ssam 	if (m)
103*54721Ssklower 		printf(" %.*s", m->m_len - (int)sizeof(sa->sun_family),
10427887Skarels 		    sa->sun_path);
10516560Ssam 	putchar('\n');
10616560Ssam }
107