xref: /csrg-svn/usr.bin/netstat/unix.c (revision 50353)
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*50353Skarels static char sccsid[] = "@(#)unix.c	5.11 (Berkeley) 07/01/91";
1034902Sbostic #endif /* not lint */
1116560Ssam 
1216560Ssam /*
1316560Ssam  * Display protocol blocks in the unix domain.
1416560Ssam  */
1516560Ssam #include <sys/param.h>
1616560Ssam #include <sys/protosw.h>
1716560Ssam #include <sys/socket.h>
1816560Ssam #include <sys/socketvar.h>
1916560Ssam #include <sys/mbuf.h>
2016560Ssam #include <sys/un.h>
2116560Ssam #include <sys/unpcb.h>
2248790Sbostic #define KERNEL
2348790Sbostic struct uio;
2416560Ssam #include <sys/file.h>
25*50353Skarels struct file *file, *fileNFILE;
26*50353Skarels int nfile;
2716560Ssam 
2816560Ssam int	Aflag;
2929750Skupfer extern	char *calloc();
3016560Ssam 
3116560Ssam unixpr(nfileaddr, fileaddr, unixsw)
3216560Ssam 	off_t nfileaddr, fileaddr;
3316560Ssam 	struct protosw *unixsw;
3416560Ssam {
3516560Ssam 	register struct file *fp;
3616560Ssam 	struct file *filep;
3716560Ssam 	struct socket sock, *so = &sock;
3816560Ssam 
3916560Ssam 	if (nfileaddr == 0 || fileaddr == 0) {
4016560Ssam 		printf("nfile or file not in namelist.\n");
4116560Ssam 		return;
4216560Ssam 	}
4343251Ssklower 	if (kvm_read(nfileaddr, (char *)&nfile, sizeof (nfile)) !=
4443251Ssklower 						sizeof (nfile)) {
4516560Ssam 		printf("nfile: bad read.\n");
4616560Ssam 		return;
4716560Ssam 	}
4843251Ssklower 	if (kvm_read(fileaddr, (char *)&filep, sizeof (filep))
4943251Ssklower 						!= sizeof (filep)) {
5016560Ssam 		printf("File table address, bad read.\n");
5116560Ssam 		return;
5216560Ssam 	}
5316560Ssam 	file = (struct file *)calloc(nfile, sizeof (struct file));
5416560Ssam 	if (file == (struct file *)0) {
5516560Ssam 		printf("Out of memory (file table).\n");
5616560Ssam 		return;
5716560Ssam 	}
5843251Ssklower 	if (kvm_read((off_t)filep, (char *)file, nfile * sizeof (struct file))
5943251Ssklower 					!= nfile * sizeof (struct file)) {
6016560Ssam 		printf("File table read error.\n");
6116560Ssam 		return;
6216560Ssam 	}
6316560Ssam 	fileNFILE = file + nfile;
6416560Ssam 	for (fp = file; fp < fileNFILE; fp++) {
6516560Ssam 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
6616560Ssam 			continue;
6743251Ssklower 		if (kvm_read((off_t)fp->f_data, (char *)so, sizeof (*so))
6843251Ssklower 					!= sizeof (*so))
6916560Ssam 			continue;
7016560Ssam 		/* kludge */
7116560Ssam 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
7216560Ssam 			if (so->so_pcb)
7316560Ssam 				unixdomainpr(so, fp->f_data);
7416560Ssam 	}
7516560Ssam 	free((char *)file);
7616560Ssam }
7716560Ssam 
7816560Ssam static	char *socktype[] =
7916560Ssam     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
8016560Ssam 
8116560Ssam unixdomainpr(so, soaddr)
8216560Ssam 	register struct socket *so;
8316560Ssam 	caddr_t soaddr;
8416560Ssam {
8516560Ssam 	struct unpcb unpcb, *unp = &unpcb;
8616560Ssam 	struct mbuf mbuf, *m;
8727887Skarels 	struct sockaddr_un *sa;
8816560Ssam 	static int first = 1;
8916560Ssam 
9043251Ssklower 	if (kvm_read((off_t)so->so_pcb, (char *)unp, sizeof (*unp))
9143251Ssklower 				!= sizeof (*unp))
9216560Ssam 		return;
9326022Smckusick 	if (unp->unp_addr) {
9416560Ssam 		m = &mbuf;
9543251Ssklower 		if (kvm_read((off_t)unp->unp_addr, (char *)m, sizeof (*m))
9643251Ssklower 				!= sizeof (*m))
9716560Ssam 			m = (struct mbuf *)0;
9839215Skarels 		sa = (struct sockaddr_un *)(m->m_dat);
9916560Ssam 	} else
10016560Ssam 		m = (struct mbuf *)0;
10116560Ssam 	if (first) {
10227887Skarels 		printf("Active UNIX domain sockets\n");
10316560Ssam 		printf(
10427887Skarels "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
10516560Ssam 		    "Address", "Type", "Recv-Q", "Send-Q",
10616560Ssam 		    "Inode", "Conn", "Refs", "Nextref");
10716560Ssam 		first = 0;
10816560Ssam 	}
10916560Ssam 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
11016560Ssam 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
11143249Skarels 	    unp->unp_vnode, unp->unp_conn,
11216560Ssam 	    unp->unp_refs, unp->unp_nextref);
11316560Ssam 	if (m)
11427887Skarels 		printf(" %.*s", m->m_len - sizeof(sa->sun_family),
11527887Skarels 		    sa->sun_path);
11616560Ssam 	putchar('\n');
11716560Ssam }
118