xref: /original-bsd/usr.bin/netstat/unix.c (revision b712016b)
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.10 (Berkeley) 04/27/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 
26 int	Aflag;
27 extern	char *calloc();
28 
29 unixpr(nfileaddr, fileaddr, unixsw)
30 	off_t nfileaddr, fileaddr;
31 	struct protosw *unixsw;
32 {
33 	register struct file *fp;
34 	struct file *filep;
35 	struct socket sock, *so = &sock;
36 
37 	if (nfileaddr == 0 || fileaddr == 0) {
38 		printf("nfile or file not in namelist.\n");
39 		return;
40 	}
41 	if (kvm_read(nfileaddr, (char *)&nfile, sizeof (nfile)) !=
42 						sizeof (nfile)) {
43 		printf("nfile: bad read.\n");
44 		return;
45 	}
46 	if (kvm_read(fileaddr, (char *)&filep, sizeof (filep))
47 						!= sizeof (filep)) {
48 		printf("File table address, bad read.\n");
49 		return;
50 	}
51 	file = (struct file *)calloc(nfile, sizeof (struct file));
52 	if (file == (struct file *)0) {
53 		printf("Out of memory (file table).\n");
54 		return;
55 	}
56 	if (kvm_read((off_t)filep, (char *)file, nfile * sizeof (struct file))
57 					!= nfile * sizeof (struct file)) {
58 		printf("File table read error.\n");
59 		return;
60 	}
61 	fileNFILE = file + nfile;
62 	for (fp = file; fp < fileNFILE; fp++) {
63 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
64 			continue;
65 		if (kvm_read((off_t)fp->f_data, (char *)so, sizeof (*so))
66 					!= sizeof (*so))
67 			continue;
68 		/* kludge */
69 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
70 			if (so->so_pcb)
71 				unixdomainpr(so, fp->f_data);
72 	}
73 	free((char *)file);
74 }
75 
76 static	char *socktype[] =
77     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
78 
79 unixdomainpr(so, soaddr)
80 	register struct socket *so;
81 	caddr_t soaddr;
82 {
83 	struct unpcb unpcb, *unp = &unpcb;
84 	struct mbuf mbuf, *m;
85 	struct sockaddr_un *sa;
86 	static int first = 1;
87 
88 	if (kvm_read((off_t)so->so_pcb, (char *)unp, sizeof (*unp))
89 				!= sizeof (*unp))
90 		return;
91 	if (unp->unp_addr) {
92 		m = &mbuf;
93 		if (kvm_read((off_t)unp->unp_addr, (char *)m, sizeof (*m))
94 				!= sizeof (*m))
95 			m = (struct mbuf *)0;
96 		sa = (struct sockaddr_un *)(m->m_dat);
97 	} else
98 		m = (struct mbuf *)0;
99 	if (first) {
100 		printf("Active UNIX domain sockets\n");
101 		printf(
102 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
103 		    "Address", "Type", "Recv-Q", "Send-Q",
104 		    "Inode", "Conn", "Refs", "Nextref");
105 		first = 0;
106 	}
107 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
108 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
109 	    unp->unp_vnode, unp->unp_conn,
110 	    unp->unp_refs, unp->unp_nextref);
111 	if (m)
112 		printf(" %.*s", m->m_len - sizeof(sa->sun_family),
113 		    sa->sun_path);
114 	putchar('\n');
115 }
116