tahoe only
[unix-history] / usr / src / usr.bin / netstat / unix.c
CommitLineData
5ff67f98
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
0b1fe039 7#ifndef lint
f7c99b06 8static char sccsid[] = "@(#)unix.c 5.4 (Berkeley) %G%";
5ff67f98 9#endif not lint
0b1fe039
SL
10
11/*
12 * Display protocol blocks in the unix domain.
13 */
14#include <sys/param.h>
15#include <sys/protosw.h>
16#include <sys/socket.h>
17#include <sys/socketvar.h>
18#include <sys/mbuf.h>
19#include <sys/un.h>
20#include <sys/unpcb.h>
21#define KERNEL
22#include <sys/file.h>
23
24int Aflag;
25int kmem;
f7c99b06 26extern char *calloc();
0b1fe039
SL
27
28unixpr(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 klseek(kmem, nfileaddr, L_SET);
f7c99b06 41 if (read(kmem, (char *)&nfile, sizeof (nfile)) != sizeof (nfile)) {
0b1fe039
SL
42 printf("nfile: bad read.\n");
43 return;
44 }
45 klseek(kmem, fileaddr, L_SET);
f7c99b06 46 if (read(kmem, (char *)&filep, sizeof (filep)) != sizeof (filep)) {
0b1fe039
SL
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 klseek(kmem, (off_t)filep, L_SET);
f7c99b06 56 if (read(kmem, (char *)file, nfile * sizeof (struct file)) !=
0b1fe039
SL
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;
f7c99b06
MK
65 klseek(kmem, (off_t)fp->f_data, L_SET);
66 if (read(kmem, (char *)so, sizeof (*so)) != sizeof (*so))
0b1fe039
SL
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
76static char *socktype[] =
77 { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
78
79unixdomainpr(so, soaddr)
80 register struct socket *so;
81 caddr_t soaddr;
82{
83 struct unpcb unpcb, *unp = &unpcb;
84 struct mbuf mbuf, *m;
2a893f10 85 struct sockaddr_un *sa;
0b1fe039
SL
86 static int first = 1;
87
f7c99b06
MK
88 klseek(kmem, (off_t)so->so_pcb, L_SET);
89 if (read(kmem, (char *)unp, sizeof (*unp)) != sizeof (*unp))
0b1fe039 90 return;
57140966 91 if (unp->unp_addr) {
0b1fe039 92 m = &mbuf;
f7c99b06
MK
93 klseek(kmem, (off_t)unp->unp_addr, L_SET);
94 if (read(kmem, (char *)m, sizeof (*m)) != sizeof (*m))
0b1fe039 95 m = (struct mbuf *)0;
2a893f10 96 sa = mtod(m, struct sockaddr_un *);
0b1fe039
SL
97 } else
98 m = (struct mbuf *)0;
99 if (first) {
2a893f10 100 printf("Active UNIX domain sockets\n");
0b1fe039 101 printf(
2a893f10 102"%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
0b1fe039
SL
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_inode, unp->unp_conn,
110 unp->unp_refs, unp->unp_nextref);
111 if (m)
2a893f10
MK
112 printf(" %.*s", m->m_len - sizeof(sa->sun_family),
113 sa->sun_path);
0b1fe039
SL
114 putchar('\n');
115}