xref: /csrg-svn/usr.bin/netstat/unix.c (revision 29750)
121991Sdist /*
221991Sdist  * Copyright (c) 1983 Regents of the University of California.
321991Sdist  * All rights reserved.  The Berkeley software License Agreement
421991Sdist  * specifies the terms and conditions for redistribution.
521991Sdist  */
621991Sdist 
716560Ssam #ifndef lint
8*29750Skupfer static char sccsid[] = "@(#)unix.c	5.4 (Berkeley) 08/11/86";
921991Sdist #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;
26*29750Skupfer 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 	}
4016560Ssam 	klseek(kmem, nfileaddr, L_SET);
41*29750Skupfer 	if (read(kmem, (char *)&nfile, sizeof (nfile)) != sizeof (nfile)) {
4216560Ssam 		printf("nfile: bad read.\n");
4316560Ssam 		return;
4416560Ssam 	}
4516560Ssam 	klseek(kmem, fileaddr, L_SET);
46*29750Skupfer 	if (read(kmem, (char *)&filep, sizeof (filep)) != 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 	}
5516560Ssam 	klseek(kmem, (off_t)filep, L_SET);
56*29750Skupfer 	if (read(kmem, (char *)file, nfile * sizeof (struct file)) !=
5716560Ssam 	    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;
65*29750Skupfer 		klseek(kmem, (off_t)fp->f_data, L_SET);
66*29750Skupfer 		if (read(kmem, (char *)so, sizeof (*so)) != 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 
88*29750Skupfer 	klseek(kmem, (off_t)so->so_pcb, L_SET);
89*29750Skupfer 	if (read(kmem, (char *)unp, sizeof (*unp)) != sizeof (*unp))
9016560Ssam 		return;
9126022Smckusick 	if (unp->unp_addr) {
9216560Ssam 		m = &mbuf;
93*29750Skupfer 		klseek(kmem, (off_t)unp->unp_addr, L_SET);
94*29750Skupfer 		if (read(kmem, (char *)m, sizeof (*m)) != sizeof (*m))
9516560Ssam 			m = (struct mbuf *)0;
9627887Skarels 		sa = mtod(m, struct sockaddr_un *);
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,
10916560Ssam 	    unp->unp_inode, 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