xref: /original-bsd/usr.bin/netstat/unix.c (revision 244eb664)
1 /*-
2  * Copyright (c) 1983, 1988 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)unix.c	5.11 (Berkeley) 07/01/91";
10 #endif /* not lint */
11 
12 /*
13  * Display protocol blocks in the unix domain.
14  */
15 #include <sys/param.h>
16 #include <sys/protosw.h>
17 #include <sys/socket.h>
18 #include <sys/socketvar.h>
19 #include <sys/mbuf.h>
20 #include <sys/un.h>
21 #include <sys/unpcb.h>
22 #define KERNEL
23 struct uio;
24 #include <sys/file.h>
25 struct file *file, *fileNFILE;
26 int nfile;
27 
28 int	Aflag;
29 extern	char *calloc();
30 
31 unixpr(nfileaddr, fileaddr, unixsw)
32 	off_t nfileaddr, fileaddr;
33 	struct protosw *unixsw;
34 {
35 	register struct file *fp;
36 	struct file *filep;
37 	struct socket sock, *so = &sock;
38 
39 	if (nfileaddr == 0 || fileaddr == 0) {
40 		printf("nfile or file not in namelist.\n");
41 		return;
42 	}
43 	if (kvm_read(nfileaddr, (char *)&nfile, sizeof (nfile)) !=
44 						sizeof (nfile)) {
45 		printf("nfile: bad read.\n");
46 		return;
47 	}
48 	if (kvm_read(fileaddr, (char *)&filep, sizeof (filep))
49 						!= sizeof (filep)) {
50 		printf("File table address, bad read.\n");
51 		return;
52 	}
53 	file = (struct file *)calloc(nfile, sizeof (struct file));
54 	if (file == (struct file *)0) {
55 		printf("Out of memory (file table).\n");
56 		return;
57 	}
58 	if (kvm_read((off_t)filep, (char *)file, nfile * sizeof (struct file))
59 					!= nfile * sizeof (struct file)) {
60 		printf("File table read error.\n");
61 		return;
62 	}
63 	fileNFILE = file + nfile;
64 	for (fp = file; fp < fileNFILE; fp++) {
65 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
66 			continue;
67 		if (kvm_read((off_t)fp->f_data, (char *)so, sizeof (*so))
68 					!= sizeof (*so))
69 			continue;
70 		/* kludge */
71 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
72 			if (so->so_pcb)
73 				unixdomainpr(so, fp->f_data);
74 	}
75 	free((char *)file);
76 }
77 
78 static	char *socktype[] =
79     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
80 
81 unixdomainpr(so, soaddr)
82 	register struct socket *so;
83 	caddr_t soaddr;
84 {
85 	struct unpcb unpcb, *unp = &unpcb;
86 	struct mbuf mbuf, *m;
87 	struct sockaddr_un *sa;
88 	static int first = 1;
89 
90 	if (kvm_read((off_t)so->so_pcb, (char *)unp, sizeof (*unp))
91 				!= sizeof (*unp))
92 		return;
93 	if (unp->unp_addr) {
94 		m = &mbuf;
95 		if (kvm_read((off_t)unp->unp_addr, (char *)m, sizeof (*m))
96 				!= sizeof (*m))
97 			m = (struct mbuf *)0;
98 		sa = (struct sockaddr_un *)(m->m_dat);
99 	} else
100 		m = (struct mbuf *)0;
101 	if (first) {
102 		printf("Active UNIX domain sockets\n");
103 		printf(
104 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
105 		    "Address", "Type", "Recv-Q", "Send-Q",
106 		    "Inode", "Conn", "Refs", "Nextref");
107 		first = 0;
108 	}
109 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
110 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
111 	    unp->unp_vnode, unp->unp_conn,
112 	    unp->unp_refs, unp->unp_nextref);
113 	if (m)
114 		printf(" %.*s", m->m_len - sizeof(sa->sun_family),
115 		    sa->sun_path);
116 	putchar('\n');
117 }
118