xref: /original-bsd/usr.bin/netstat/unix.c (revision b3f911e0)
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 the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)unix.c	5.7 (Berkeley) 09/25/89";
20 #endif /* not lint */
21 
22 /*
23  * Display protocol blocks in the unix domain.
24  */
25 #include <sys/param.h>
26 #include <sys/protosw.h>
27 #include <sys/socket.h>
28 #include <sys/socketvar.h>
29 #include <sys/mbuf.h>
30 #include <sys/un.h>
31 #include <sys/unpcb.h>
32 #define	KERNEL
33 #include <sys/file.h>
34 
35 int	Aflag;
36 int	kmem;
37 extern	char *calloc();
38 
39 unixpr(nfileaddr, fileaddr, unixsw)
40 	off_t nfileaddr, fileaddr;
41 	struct protosw *unixsw;
42 {
43 	register struct file *fp;
44 	struct file *filep;
45 	struct socket sock, *so = &sock;
46 
47 	if (nfileaddr == 0 || fileaddr == 0) {
48 		printf("nfile or file not in namelist.\n");
49 		return;
50 	}
51 	klseek(kmem, nfileaddr, L_SET);
52 	if (read(kmem, (char *)&nfile, sizeof (nfile)) != sizeof (nfile)) {
53 		printf("nfile: bad read.\n");
54 		return;
55 	}
56 	klseek(kmem, fileaddr, L_SET);
57 	if (read(kmem, (char *)&filep, sizeof (filep)) != sizeof (filep)) {
58 		printf("File table address, bad read.\n");
59 		return;
60 	}
61 	file = (struct file *)calloc(nfile, sizeof (struct file));
62 	if (file == (struct file *)0) {
63 		printf("Out of memory (file table).\n");
64 		return;
65 	}
66 	klseek(kmem, (off_t)filep, L_SET);
67 	if (read(kmem, (char *)file, nfile * sizeof (struct file)) !=
68 	    nfile * sizeof (struct file)) {
69 		printf("File table read error.\n");
70 		return;
71 	}
72 	fileNFILE = file + nfile;
73 	for (fp = file; fp < fileNFILE; fp++) {
74 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
75 			continue;
76 		klseek(kmem, (off_t)fp->f_data, L_SET);
77 		if (read(kmem, (char *)so, sizeof (*so)) != sizeof (*so))
78 			continue;
79 		/* kludge */
80 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
81 			if (so->so_pcb)
82 				unixdomainpr(so, fp->f_data);
83 	}
84 	free((char *)file);
85 }
86 
87 static	char *socktype[] =
88     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
89 
90 unixdomainpr(so, soaddr)
91 	register struct socket *so;
92 	caddr_t soaddr;
93 {
94 	struct unpcb unpcb, *unp = &unpcb;
95 	struct mbuf mbuf, *m;
96 	struct sockaddr_un *sa;
97 	static int first = 1;
98 
99 	klseek(kmem, (off_t)so->so_pcb, L_SET);
100 	if (read(kmem, (char *)unp, sizeof (*unp)) != sizeof (*unp))
101 		return;
102 	if (unp->unp_addr) {
103 		m = &mbuf;
104 		klseek(kmem, (off_t)unp->unp_addr, L_SET);
105 		if (read(kmem, (char *)m, sizeof (*m)) != sizeof (*m))
106 			m = (struct mbuf *)0;
107 		sa = (struct sockaddr_un *)(m->m_dat);
108 	} else
109 		m = (struct mbuf *)0;
110 	if (first) {
111 		printf("Active UNIX domain sockets\n");
112 		printf(
113 "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
114 		    "Address", "Type", "Recv-Q", "Send-Q",
115 		    "Inode", "Conn", "Refs", "Nextref");
116 		first = 0;
117 	}
118 	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
119 	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
120 	    unp->unp_inode, unp->unp_conn,
121 	    unp->unp_refs, unp->unp_nextref);
122 	if (m)
123 		printf(" %.*s", m->m_len - sizeof(sa->sun_family),
124 		    sa->sun_path);
125 	putchar('\n');
126 }
127