xref: /csrg-svn/usr.bin/netstat/unix.c (revision 21991)
1*21991Sdist /*
2*21991Sdist  * Copyright (c) 1983 Regents of the University of California.
3*21991Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21991Sdist  * specifies the terms and conditions for redistribution.
5*21991Sdist  */
6*21991Sdist 
716560Ssam #ifndef lint
8*21991Sdist static char sccsid[] = "@(#)unix.c	5.1 (Berkeley) 06/04/85";
9*21991Sdist #endif not lint
1016560Ssam 
1116560Ssam /*
1216560Ssam  * Display protocol blocks in the unix domain.
1316560Ssam  */
1416560Ssam #include <sys/param.h>
1516560Ssam #include <sys/protosw.h>
1616560Ssam #include <sys/socket.h>
1716560Ssam #include <sys/socketvar.h>
1816560Ssam #include <sys/mbuf.h>
1916560Ssam #include <sys/un.h>
2016560Ssam #include <sys/unpcb.h>
2116560Ssam #define	KERNEL
2216560Ssam #include <sys/file.h>
2316560Ssam 
2416560Ssam int	Aflag;
2516560Ssam int	kmem;
2616560Ssam 
2716560Ssam unixpr(nfileaddr, fileaddr, unixsw)
2816560Ssam 	off_t nfileaddr, fileaddr;
2916560Ssam 	struct protosw *unixsw;
3016560Ssam {
3116560Ssam 	register struct file *fp;
3216560Ssam 	struct file *filep;
3316560Ssam 	struct socket sock, *so = &sock;
3416560Ssam 
3516560Ssam 	if (nfileaddr == 0 || fileaddr == 0) {
3616560Ssam 		printf("nfile or file not in namelist.\n");
3716560Ssam 		return;
3816560Ssam 	}
3916560Ssam 	klseek(kmem, nfileaddr, L_SET);
4016560Ssam 	if (read(kmem, &nfile, sizeof (nfile)) != sizeof (nfile)) {
4116560Ssam 		printf("nfile: bad read.\n");
4216560Ssam 		return;
4316560Ssam 	}
4416560Ssam 	klseek(kmem, fileaddr, L_SET);
4516560Ssam 	if (read(kmem, &filep, sizeof (filep)) != sizeof (filep)) {
4616560Ssam 		printf("File table address, bad read.\n");
4716560Ssam 		return;
4816560Ssam 	}
4916560Ssam 	file = (struct file *)calloc(nfile, sizeof (struct file));
5016560Ssam 	if (file == (struct file *)0) {
5116560Ssam 		printf("Out of memory (file table).\n");
5216560Ssam 		return;
5316560Ssam 	}
5416560Ssam 	klseek(kmem, (off_t)filep, L_SET);
5516560Ssam 	if (read(kmem, file, nfile * sizeof (struct file)) !=
5616560Ssam 	    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;
6416560Ssam 		klseek(kmem, fp->f_data, L_SET);
6516560Ssam 		if (read(kmem, so, sizeof (*so)) != 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;
8416560Ssam 	static int first = 1;
8516560Ssam 
8616560Ssam 	klseek(kmem, so->so_pcb, L_SET);
8716560Ssam 	if (read(kmem, unp, sizeof (*unp)) != sizeof (*unp))
8816560Ssam 		return;
8916560Ssam 	if (unp->unp_remaddr) {
9016560Ssam 		m = &mbuf;
9116560Ssam 		klseek(kmem, unp->unp_remaddr, L_SET);
9216560Ssam 		if (read(kmem, m, sizeof (*m)) != sizeof (*m))
9316560Ssam 			m = (struct mbuf *)0;
9416560Ssam 	} else
9516560Ssam 		m = (struct mbuf *)0;
9616560Ssam 	if (first) {
9716560Ssam 		printf(
9816560Ssam "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Remaddr\n",
9916560Ssam 		    "Address", "Type", "Recv-Q", "Send-Q",
10016560Ssam 		    "Inode", "Conn", "Refs", "Nextref");
10116560Ssam 		first = 0;
10216560Ssam 	}
10316560Ssam 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
10416560Ssam 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
10516560Ssam 	    unp->unp_inode, unp->unp_conn,
10616560Ssam 	    unp->unp_refs, unp->unp_nextref);
10716560Ssam 	if (m)
10816560Ssam 		printf(" %.*s", m->m_len, mtod(m, char *));
10916560Ssam 	putchar('\n');
11016560Ssam }
111