xref: /csrg-svn/usr.bin/netstat/unix.c (revision 62145)
143249Skarels /*-
2*62145Sbostic  * Copyright (c) 1983, 1988, 1993
3*62145Sbostic  *	The Regents of the University of California.  All rights reserved.
433451Skarels  *
543249Skarels  * %sccs.include.redist.c%
621991Sdist  */
721991Sdist 
816560Ssam #ifndef lint
9*62145Sbostic static char sccsid[] = "@(#)unix.c	8.1 (Berkeley) 06/06/93";
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>
2158931Smckusick #include <sys/sysctl.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>
2854721Ssklower 
2954721Ssklower #include <netinet/in.h>
3054721Ssklower 
3154721Ssklower #include <stdio.h>
3253698Ssklower #include <stdlib.h>
3353698Ssklower #include "netstat.h"
3453698Ssklower 
3554721Ssklower static	void unixdomainpr __P((struct socket *, caddr_t));
3653698Ssklower 
3758076Storek static struct	file *file, *fileNFILE;
3858076Storek static int	nfiles;
3953698Ssklower extern	kvm_t *kvmd;
4016560Ssam 
4153698Ssklower void
unixpr(off)4254761Ssklower unixpr(off)
4354761Ssklower 	u_long	off;
4416560Ssam {
4516560Ssam 	register struct file *fp;
4616560Ssam 	struct socket sock, *so = &sock;
4753698Ssklower 	char *filebuf;
4854761Ssklower 	struct protosw *unixsw = (struct protosw *)off;
4916560Ssam 
5058931Smckusick 	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
5153698Ssklower 	if (filebuf == 0) {
5216560Ssam 		printf("Out of memory (file table).\n");
5316560Ssam 		return;
5416560Ssam 	}
5553698Ssklower 	file = (struct file *)(filebuf + sizeof(fp));
5653698Ssklower 	fileNFILE = file + nfiles;
5716560Ssam 	for (fp = file; fp < fileNFILE; fp++) {
5816560Ssam 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
5916560Ssam 			continue;
6054761Ssklower 		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
6116560Ssam 			continue;
6216560Ssam 		/* kludge */
6316560Ssam 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
6416560Ssam 			if (so->so_pcb)
6516560Ssam 				unixdomainpr(so, fp->f_data);
6616560Ssam 	}
6716560Ssam }
6816560Ssam 
6916560Ssam static	char *socktype[] =
7016560Ssam     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
7116560Ssam 
7253698Ssklower static void
unixdomainpr(so,soaddr)7316560Ssam unixdomainpr(so, soaddr)
7416560Ssam 	register struct socket *so;
7516560Ssam 	caddr_t soaddr;
7616560Ssam {
7716560Ssam 	struct unpcb unpcb, *unp = &unpcb;
7816560Ssam 	struct mbuf mbuf, *m;
7927887Skarels 	struct sockaddr_un *sa;
8016560Ssam 	static int first = 1;
8116560Ssam 
8254761Ssklower 	if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
8316560Ssam 		return;
8426022Smckusick 	if (unp->unp_addr) {
8516560Ssam 		m = &mbuf;
8654761Ssklower 		if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
8716560Ssam 			m = (struct mbuf *)0;
8839215Skarels 		sa = (struct sockaddr_un *)(m->m_dat);
8916560Ssam 	} else
9016560Ssam 		m = (struct mbuf *)0;
9116560Ssam 	if (first) {
9227887Skarels 		printf("Active UNIX domain sockets\n");
9316560Ssam 		printf(
9427887Skarels "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
9516560Ssam 		    "Address", "Type", "Recv-Q", "Send-Q",
9616560Ssam 		    "Inode", "Conn", "Refs", "Nextref");
9716560Ssam 		first = 0;
9816560Ssam 	}
9916560Ssam 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
10016560Ssam 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
10143249Skarels 	    unp->unp_vnode, unp->unp_conn,
10216560Ssam 	    unp->unp_refs, unp->unp_nextref);
10316560Ssam 	if (m)
10458076Storek 		printf(" %.*s",
10558076Storek 		    m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path)),
10627887Skarels 		    sa->sun_path);
10716560Ssam 	putchar('\n');
10816560Ssam }
109