xref: /csrg-svn/usr.bin/netstat/unix.c (revision 48790)
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*48790Sbostic static char sccsid[] = "@(#)unix.c	5.10 (Berkeley) 04/27/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>
22*48790Sbostic #define KERNEL
23*48790Sbostic struct uio;
2416560Ssam #include <sys/file.h>
2516560Ssam 
2616560Ssam int	Aflag;
2729750Skupfer extern	char *calloc();
2816560Ssam 
2916560Ssam unixpr(nfileaddr, fileaddr, unixsw)
3016560Ssam 	off_t nfileaddr, fileaddr;
3116560Ssam 	struct protosw *unixsw;
3216560Ssam {
3316560Ssam 	register struct file *fp;
3416560Ssam 	struct file *filep;
3516560Ssam 	struct socket sock, *so = &sock;
3616560Ssam 
3716560Ssam 	if (nfileaddr == 0 || fileaddr == 0) {
3816560Ssam 		printf("nfile or file not in namelist.\n");
3916560Ssam 		return;
4016560Ssam 	}
4143251Ssklower 	if (kvm_read(nfileaddr, (char *)&nfile, sizeof (nfile)) !=
4243251Ssklower 						sizeof (nfile)) {
4316560Ssam 		printf("nfile: bad read.\n");
4416560Ssam 		return;
4516560Ssam 	}
4643251Ssklower 	if (kvm_read(fileaddr, (char *)&filep, sizeof (filep))
4743251Ssklower 						!= sizeof (filep)) {
4816560Ssam 		printf("File table address, bad read.\n");
4916560Ssam 		return;
5016560Ssam 	}
5116560Ssam 	file = (struct file *)calloc(nfile, sizeof (struct file));
5216560Ssam 	if (file == (struct file *)0) {
5316560Ssam 		printf("Out of memory (file table).\n");
5416560Ssam 		return;
5516560Ssam 	}
5643251Ssklower 	if (kvm_read((off_t)filep, (char *)file, nfile * sizeof (struct file))
5743251Ssklower 					!= nfile * sizeof (struct file)) {
5816560Ssam 		printf("File table read error.\n");
5916560Ssam 		return;
6016560Ssam 	}
6116560Ssam 	fileNFILE = file + nfile;
6216560Ssam 	for (fp = file; fp < fileNFILE; fp++) {
6316560Ssam 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
6416560Ssam 			continue;
6543251Ssklower 		if (kvm_read((off_t)fp->f_data, (char *)so, sizeof (*so))
6643251Ssklower 					!= sizeof (*so))
6716560Ssam 			continue;
6816560Ssam 		/* kludge */
6916560Ssam 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
7016560Ssam 			if (so->so_pcb)
7116560Ssam 				unixdomainpr(so, fp->f_data);
7216560Ssam 	}
7316560Ssam 	free((char *)file);
7416560Ssam }
7516560Ssam 
7616560Ssam static	char *socktype[] =
7716560Ssam     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
7816560Ssam 
7916560Ssam unixdomainpr(so, soaddr)
8016560Ssam 	register struct socket *so;
8116560Ssam 	caddr_t soaddr;
8216560Ssam {
8316560Ssam 	struct unpcb unpcb, *unp = &unpcb;
8416560Ssam 	struct mbuf mbuf, *m;
8527887Skarels 	struct sockaddr_un *sa;
8616560Ssam 	static int first = 1;
8716560Ssam 
8843251Ssklower 	if (kvm_read((off_t)so->so_pcb, (char *)unp, sizeof (*unp))
8943251Ssklower 				!= sizeof (*unp))
9016560Ssam 		return;
9126022Smckusick 	if (unp->unp_addr) {
9216560Ssam 		m = &mbuf;
9343251Ssklower 		if (kvm_read((off_t)unp->unp_addr, (char *)m, sizeof (*m))
9443251Ssklower 				!= sizeof (*m))
9516560Ssam 			m = (struct mbuf *)0;
9639215Skarels 		sa = (struct sockaddr_un *)(m->m_dat);
9716560Ssam 	} else
9816560Ssam 		m = (struct mbuf *)0;
9916560Ssam 	if (first) {
10027887Skarels 		printf("Active UNIX domain sockets\n");
10116560Ssam 		printf(
10227887Skarels "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
10316560Ssam 		    "Address", "Type", "Recv-Q", "Send-Q",
10416560Ssam 		    "Inode", "Conn", "Refs", "Nextref");
10516560Ssam 		first = 0;
10616560Ssam 	}
10716560Ssam 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
10816560Ssam 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
10943249Skarels 	    unp->unp_vnode, unp->unp_conn,
11016560Ssam 	    unp->unp_refs, unp->unp_nextref);
11116560Ssam 	if (m)
11227887Skarels 		printf(" %.*s", m->m_len - sizeof(sa->sun_family),
11327887Skarels 		    sa->sun_path);
11416560Ssam 	putchar('\n');
11516560Ssam }
116