xref: /csrg-svn/usr.bin/netstat/unix.c (revision 39215)
121991Sdist /*
234902Sbostic  * Copyright (c) 1983, 1988 Regents of the University of California.
333451Skarels  * All rights reserved.
433451Skarels  *
533451Skarels  * Redistribution and use in source and binary forms are permitted
634902Sbostic  * provided that the above copyright notice and this paragraph are
734902Sbostic  * duplicated in all such forms and that any documentation,
834902Sbostic  * advertising materials, and other materials related to such
934902Sbostic  * distribution and use acknowledge that the software was developed
1034902Sbostic  * by the University of California, Berkeley.  The name of the
1134902Sbostic  * University may not be used to endorse or promote products derived
1234902Sbostic  * from this software without specific prior written permission.
1334902Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434902Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534902Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621991Sdist  */
1721991Sdist 
1816560Ssam #ifndef lint
19*39215Skarels static char sccsid[] = "@(#)unix.c	5.7 (Berkeley) 09/25/89";
2034902Sbostic #endif /* not lint */
2116560Ssam 
2216560Ssam /*
2316560Ssam  * Display protocol blocks in the unix domain.
2416560Ssam  */
2516560Ssam #include <sys/param.h>
2616560Ssam #include <sys/protosw.h>
2716560Ssam #include <sys/socket.h>
2816560Ssam #include <sys/socketvar.h>
2916560Ssam #include <sys/mbuf.h>
3016560Ssam #include <sys/un.h>
3116560Ssam #include <sys/unpcb.h>
3216560Ssam #define	KERNEL
3316560Ssam #include <sys/file.h>
3416560Ssam 
3516560Ssam int	Aflag;
3616560Ssam int	kmem;
3729750Skupfer extern	char *calloc();
3816560Ssam 
3916560Ssam unixpr(nfileaddr, fileaddr, unixsw)
4016560Ssam 	off_t nfileaddr, fileaddr;
4116560Ssam 	struct protosw *unixsw;
4216560Ssam {
4316560Ssam 	register struct file *fp;
4416560Ssam 	struct file *filep;
4516560Ssam 	struct socket sock, *so = &sock;
4616560Ssam 
4716560Ssam 	if (nfileaddr == 0 || fileaddr == 0) {
4816560Ssam 		printf("nfile or file not in namelist.\n");
4916560Ssam 		return;
5016560Ssam 	}
5116560Ssam 	klseek(kmem, nfileaddr, L_SET);
5229750Skupfer 	if (read(kmem, (char *)&nfile, sizeof (nfile)) != sizeof (nfile)) {
5316560Ssam 		printf("nfile: bad read.\n");
5416560Ssam 		return;
5516560Ssam 	}
5616560Ssam 	klseek(kmem, fileaddr, L_SET);
5729750Skupfer 	if (read(kmem, (char *)&filep, sizeof (filep)) != sizeof (filep)) {
5816560Ssam 		printf("File table address, bad read.\n");
5916560Ssam 		return;
6016560Ssam 	}
6116560Ssam 	file = (struct file *)calloc(nfile, sizeof (struct file));
6216560Ssam 	if (file == (struct file *)0) {
6316560Ssam 		printf("Out of memory (file table).\n");
6416560Ssam 		return;
6516560Ssam 	}
6616560Ssam 	klseek(kmem, (off_t)filep, L_SET);
6729750Skupfer 	if (read(kmem, (char *)file, nfile * sizeof (struct file)) !=
6816560Ssam 	    nfile * sizeof (struct file)) {
6916560Ssam 		printf("File table read error.\n");
7016560Ssam 		return;
7116560Ssam 	}
7216560Ssam 	fileNFILE = file + nfile;
7316560Ssam 	for (fp = file; fp < fileNFILE; fp++) {
7416560Ssam 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
7516560Ssam 			continue;
7629750Skupfer 		klseek(kmem, (off_t)fp->f_data, L_SET);
7729750Skupfer 		if (read(kmem, (char *)so, sizeof (*so)) != sizeof (*so))
7816560Ssam 			continue;
7916560Ssam 		/* kludge */
8016560Ssam 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
8116560Ssam 			if (so->so_pcb)
8216560Ssam 				unixdomainpr(so, fp->f_data);
8316560Ssam 	}
8416560Ssam 	free((char *)file);
8516560Ssam }
8616560Ssam 
8716560Ssam static	char *socktype[] =
8816560Ssam     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
8916560Ssam 
9016560Ssam unixdomainpr(so, soaddr)
9116560Ssam 	register struct socket *so;
9216560Ssam 	caddr_t soaddr;
9316560Ssam {
9416560Ssam 	struct unpcb unpcb, *unp = &unpcb;
9516560Ssam 	struct mbuf mbuf, *m;
9627887Skarels 	struct sockaddr_un *sa;
9716560Ssam 	static int first = 1;
9816560Ssam 
9929750Skupfer 	klseek(kmem, (off_t)so->so_pcb, L_SET);
10029750Skupfer 	if (read(kmem, (char *)unp, sizeof (*unp)) != sizeof (*unp))
10116560Ssam 		return;
10226022Smckusick 	if (unp->unp_addr) {
10316560Ssam 		m = &mbuf;
10429750Skupfer 		klseek(kmem, (off_t)unp->unp_addr, L_SET);
10529750Skupfer 		if (read(kmem, (char *)m, sizeof (*m)) != sizeof (*m))
10616560Ssam 			m = (struct mbuf *)0;
107*39215Skarels 		sa = (struct sockaddr_un *)(m->m_dat);
10816560Ssam 	} else
10916560Ssam 		m = (struct mbuf *)0;
11016560Ssam 	if (first) {
11127887Skarels 		printf("Active UNIX domain sockets\n");
11216560Ssam 		printf(
11327887Skarels "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
11416560Ssam 		    "Address", "Type", "Recv-Q", "Send-Q",
11516560Ssam 		    "Inode", "Conn", "Refs", "Nextref");
11616560Ssam 		first = 0;
11716560Ssam 	}
11816560Ssam 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
11916560Ssam 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
12016560Ssam 	    unp->unp_inode, unp->unp_conn,
12116560Ssam 	    unp->unp_refs, unp->unp_nextref);
12216560Ssam 	if (m)
12327887Skarels 		printf(" %.*s", m->m_len - sizeof(sa->sun_family),
12427887Skarels 		    sa->sun_path);
12516560Ssam 	putchar('\n');
12616560Ssam }
127