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