xref: /csrg-svn/usr.bin/netstat/unix.c (revision 43251)
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*43251Ssklower static char sccsid[] = "@(#)unix.c	5.9 (Berkeley) 06/18/90";
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>
2216560Ssam #define	KERNEL
2316560Ssam #include <sys/file.h>
2416560Ssam 
2516560Ssam int	Aflag;
2629750Skupfer extern	char *calloc();
2716560Ssam 
2816560Ssam unixpr(nfileaddr, fileaddr, unixsw)
2916560Ssam 	off_t nfileaddr, fileaddr;
3016560Ssam 	struct protosw *unixsw;
3116560Ssam {
3216560Ssam 	register struct file *fp;
3316560Ssam 	struct file *filep;
3416560Ssam 	struct socket sock, *so = &sock;
3516560Ssam 
3616560Ssam 	if (nfileaddr == 0 || fileaddr == 0) {
3716560Ssam 		printf("nfile or file not in namelist.\n");
3816560Ssam 		return;
3916560Ssam 	}
40*43251Ssklower 	if (kvm_read(nfileaddr, (char *)&nfile, sizeof (nfile)) !=
41*43251Ssklower 						sizeof (nfile)) {
4216560Ssam 		printf("nfile: bad read.\n");
4316560Ssam 		return;
4416560Ssam 	}
45*43251Ssklower 	if (kvm_read(fileaddr, (char *)&filep, sizeof (filep))
46*43251Ssklower 						!= sizeof (filep)) {
4716560Ssam 		printf("File table address, bad read.\n");
4816560Ssam 		return;
4916560Ssam 	}
5016560Ssam 	file = (struct file *)calloc(nfile, sizeof (struct file));
5116560Ssam 	if (file == (struct file *)0) {
5216560Ssam 		printf("Out of memory (file table).\n");
5316560Ssam 		return;
5416560Ssam 	}
55*43251Ssklower 	if (kvm_read((off_t)filep, (char *)file, nfile * sizeof (struct file))
56*43251Ssklower 					!= nfile * sizeof (struct file)) {
5716560Ssam 		printf("File table read error.\n");
5816560Ssam 		return;
5916560Ssam 	}
6016560Ssam 	fileNFILE = file + nfile;
6116560Ssam 	for (fp = file; fp < fileNFILE; fp++) {
6216560Ssam 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
6316560Ssam 			continue;
64*43251Ssklower 		if (kvm_read((off_t)fp->f_data, (char *)so, sizeof (*so))
65*43251Ssklower 					!= sizeof (*so))
6616560Ssam 			continue;
6716560Ssam 		/* kludge */
6816560Ssam 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
6916560Ssam 			if (so->so_pcb)
7016560Ssam 				unixdomainpr(so, fp->f_data);
7116560Ssam 	}
7216560Ssam 	free((char *)file);
7316560Ssam }
7416560Ssam 
7516560Ssam static	char *socktype[] =
7616560Ssam     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
7716560Ssam 
7816560Ssam unixdomainpr(so, soaddr)
7916560Ssam 	register struct socket *so;
8016560Ssam 	caddr_t soaddr;
8116560Ssam {
8216560Ssam 	struct unpcb unpcb, *unp = &unpcb;
8316560Ssam 	struct mbuf mbuf, *m;
8427887Skarels 	struct sockaddr_un *sa;
8516560Ssam 	static int first = 1;
8616560Ssam 
87*43251Ssklower 	if (kvm_read((off_t)so->so_pcb, (char *)unp, sizeof (*unp))
88*43251Ssklower 				!= sizeof (*unp))
8916560Ssam 		return;
9026022Smckusick 	if (unp->unp_addr) {
9116560Ssam 		m = &mbuf;
92*43251Ssklower 		if (kvm_read((off_t)unp->unp_addr, (char *)m, sizeof (*m))
93*43251Ssklower 				!= sizeof (*m))
9416560Ssam 			m = (struct mbuf *)0;
9539215Skarels 		sa = (struct sockaddr_un *)(m->m_dat);
9616560Ssam 	} else
9716560Ssam 		m = (struct mbuf *)0;
9816560Ssam 	if (first) {
9927887Skarels 		printf("Active UNIX domain sockets\n");
10016560Ssam 		printf(
10127887Skarels "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
10216560Ssam 		    "Address", "Type", "Recv-Q", "Send-Q",
10316560Ssam 		    "Inode", "Conn", "Refs", "Nextref");
10416560Ssam 		first = 0;
10516560Ssam 	}
10616560Ssam 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
10716560Ssam 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
10843249Skarels 	    unp->unp_vnode, unp->unp_conn,
10916560Ssam 	    unp->unp_refs, unp->unp_nextref);
11016560Ssam 	if (m)
11127887Skarels 		printf(" %.*s", m->m_len - sizeof(sa->sun_family),
11227887Skarels 		    sa->sun_path);
11316560Ssam 	putchar('\n');
11416560Ssam }
115