xref: /csrg-svn/usr.bin/netstat/unix.c (revision 33451)
121991Sdist /*
2*33451Skarels  * Copyright (c) 1983,1988 Regents of the University of California.
3*33451Skarels  * All rights reserved.
4*33451Skarels  *
5*33451Skarels  * Redistribution and use in source and binary forms are permitted
6*33451Skarels  * provided that this notice is preserved and that due credit is given
7*33451Skarels  * to the University of California at Berkeley. The name of the University
8*33451Skarels  * may not be used to endorse or promote products derived from this
9*33451Skarels  * software without specific prior written permission. This software
10*33451Skarels  * is provided ``as is'' without express or implied warranty.
1121991Sdist  */
1221991Sdist 
1316560Ssam #ifndef lint
14*33451Skarels static char sccsid[] = "@(#)unix.c	5.5 (Berkeley) 02/07/88";
1521991Sdist #endif not lint
1616560Ssam 
1716560Ssam /*
1816560Ssam  * Display protocol blocks in the unix domain.
1916560Ssam  */
2016560Ssam #include <sys/param.h>
2116560Ssam #include <sys/protosw.h>
2216560Ssam #include <sys/socket.h>
2316560Ssam #include <sys/socketvar.h>
2416560Ssam #include <sys/mbuf.h>
2516560Ssam #include <sys/un.h>
2616560Ssam #include <sys/unpcb.h>
2716560Ssam #define	KERNEL
2816560Ssam #include <sys/file.h>
2916560Ssam 
3016560Ssam int	Aflag;
3116560Ssam int	kmem;
3229750Skupfer extern	char *calloc();
3316560Ssam 
3416560Ssam unixpr(nfileaddr, fileaddr, unixsw)
3516560Ssam 	off_t nfileaddr, fileaddr;
3616560Ssam 	struct protosw *unixsw;
3716560Ssam {
3816560Ssam 	register struct file *fp;
3916560Ssam 	struct file *filep;
4016560Ssam 	struct socket sock, *so = &sock;
4116560Ssam 
4216560Ssam 	if (nfileaddr == 0 || fileaddr == 0) {
4316560Ssam 		printf("nfile or file not in namelist.\n");
4416560Ssam 		return;
4516560Ssam 	}
4616560Ssam 	klseek(kmem, nfileaddr, L_SET);
4729750Skupfer 	if (read(kmem, (char *)&nfile, sizeof (nfile)) != sizeof (nfile)) {
4816560Ssam 		printf("nfile: bad read.\n");
4916560Ssam 		return;
5016560Ssam 	}
5116560Ssam 	klseek(kmem, fileaddr, L_SET);
5229750Skupfer 	if (read(kmem, (char *)&filep, sizeof (filep)) != sizeof (filep)) {
5316560Ssam 		printf("File table address, bad read.\n");
5416560Ssam 		return;
5516560Ssam 	}
5616560Ssam 	file = (struct file *)calloc(nfile, sizeof (struct file));
5716560Ssam 	if (file == (struct file *)0) {
5816560Ssam 		printf("Out of memory (file table).\n");
5916560Ssam 		return;
6016560Ssam 	}
6116560Ssam 	klseek(kmem, (off_t)filep, L_SET);
6229750Skupfer 	if (read(kmem, (char *)file, nfile * sizeof (struct file)) !=
6316560Ssam 	    nfile * sizeof (struct file)) {
6416560Ssam 		printf("File table read error.\n");
6516560Ssam 		return;
6616560Ssam 	}
6716560Ssam 	fileNFILE = file + nfile;
6816560Ssam 	for (fp = file; fp < fileNFILE; fp++) {
6916560Ssam 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
7016560Ssam 			continue;
7129750Skupfer 		klseek(kmem, (off_t)fp->f_data, L_SET);
7229750Skupfer 		if (read(kmem, (char *)so, sizeof (*so)) != sizeof (*so))
7316560Ssam 			continue;
7416560Ssam 		/* kludge */
7516560Ssam 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
7616560Ssam 			if (so->so_pcb)
7716560Ssam 				unixdomainpr(so, fp->f_data);
7816560Ssam 	}
7916560Ssam 	free((char *)file);
8016560Ssam }
8116560Ssam 
8216560Ssam static	char *socktype[] =
8316560Ssam     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
8416560Ssam 
8516560Ssam unixdomainpr(so, soaddr)
8616560Ssam 	register struct socket *so;
8716560Ssam 	caddr_t soaddr;
8816560Ssam {
8916560Ssam 	struct unpcb unpcb, *unp = &unpcb;
9016560Ssam 	struct mbuf mbuf, *m;
9127887Skarels 	struct sockaddr_un *sa;
9216560Ssam 	static int first = 1;
9316560Ssam 
9429750Skupfer 	klseek(kmem, (off_t)so->so_pcb, L_SET);
9529750Skupfer 	if (read(kmem, (char *)unp, sizeof (*unp)) != sizeof (*unp))
9616560Ssam 		return;
9726022Smckusick 	if (unp->unp_addr) {
9816560Ssam 		m = &mbuf;
9929750Skupfer 		klseek(kmem, (off_t)unp->unp_addr, L_SET);
10029750Skupfer 		if (read(kmem, (char *)m, sizeof (*m)) != sizeof (*m))
10116560Ssam 			m = (struct mbuf *)0;
10227887Skarels 		sa = mtod(m, struct sockaddr_un *);
10316560Ssam 	} else
10416560Ssam 		m = (struct mbuf *)0;
10516560Ssam 	if (first) {
10627887Skarels 		printf("Active UNIX domain sockets\n");
10716560Ssam 		printf(
10827887Skarels "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
10916560Ssam 		    "Address", "Type", "Recv-Q", "Send-Q",
11016560Ssam 		    "Inode", "Conn", "Refs", "Nextref");
11116560Ssam 		first = 0;
11216560Ssam 	}
11316560Ssam 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
11416560Ssam 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
11516560Ssam 	    unp->unp_inode, unp->unp_conn,
11616560Ssam 	    unp->unp_refs, unp->unp_nextref);
11716560Ssam 	if (m)
11827887Skarels 		printf(" %.*s", m->m_len - sizeof(sa->sun_family),
11927887Skarels 		    sa->sun_path);
12016560Ssam 	putchar('\n');
12116560Ssam }
122