less -> more
[unix-history] / usr / src / usr.bin / netstat / unix.c
CommitLineData
5ff67f98 1/*
b36fc510 2 * Copyright (c) 1983, 1988 Regents of the University of California.
ee3f90a5
MK
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
b36fc510
KB
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.
5ff67f98
DF
16 */
17
0b1fe039 18#ifndef lint
b36fc510
KB
19static char sccsid[] = "@(#)unix.c 5.6 (Berkeley) %G%";
20#endif /* not lint */
0b1fe039
SL
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
35int Aflag;
36int kmem;
f7c99b06 37extern char *calloc();
0b1fe039
SL
38
39unixpr(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);
f7c99b06 52 if (read(kmem, (char *)&nfile, sizeof (nfile)) != sizeof (nfile)) {
0b1fe039
SL
53 printf("nfile: bad read.\n");
54 return;
55 }
56 klseek(kmem, fileaddr, L_SET);
f7c99b06 57 if (read(kmem, (char *)&filep, sizeof (filep)) != sizeof (filep)) {
0b1fe039
SL
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);
f7c99b06 67 if (read(kmem, (char *)file, nfile * sizeof (struct file)) !=
0b1fe039
SL
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;
f7c99b06
MK
76 klseek(kmem, (off_t)fp->f_data, L_SET);
77 if (read(kmem, (char *)so, sizeof (*so)) != sizeof (*so))
0b1fe039
SL
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
87static char *socktype[] =
88 { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
89
90unixdomainpr(so, soaddr)
91 register struct socket *so;
92 caddr_t soaddr;
93{
94 struct unpcb unpcb, *unp = &unpcb;
95 struct mbuf mbuf, *m;
2a893f10 96 struct sockaddr_un *sa;
0b1fe039
SL
97 static int first = 1;
98
f7c99b06
MK
99 klseek(kmem, (off_t)so->so_pcb, L_SET);
100 if (read(kmem, (char *)unp, sizeof (*unp)) != sizeof (*unp))
0b1fe039 101 return;
57140966 102 if (unp->unp_addr) {
0b1fe039 103 m = &mbuf;
f7c99b06
MK
104 klseek(kmem, (off_t)unp->unp_addr, L_SET);
105 if (read(kmem, (char *)m, sizeof (*m)) != sizeof (*m))
0b1fe039 106 m = (struct mbuf *)0;
2a893f10 107 sa = mtod(m, struct sockaddr_un *);
0b1fe039
SL
108 } else
109 m = (struct mbuf *)0;
110 if (first) {
2a893f10 111 printf("Active UNIX domain sockets\n");
0b1fe039 112 printf(
2a893f10 113"%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
0b1fe039
SL
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)
2a893f10
MK
123 printf(" %.*s", m->m_len - sizeof(sa->sun_family),
124 sa->sun_path);
0b1fe039
SL
125 putchar('\n');
126}