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