Initial import, 0.1 + pk 0.2.4-B1
[unix-history] / usr.bin / netstat / unix.c
CommitLineData
15637ed4
RG
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, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)unix.c 5.11 (Berkeley) 7/1/91";
36#endif /* not lint */
37
38/*
39 * Display protocol blocks in the unix domain.
40 */
41#include <sys/param.h>
42#include <sys/protosw.h>
43#include <sys/socket.h>
44#include <sys/socketvar.h>
45#include <sys/mbuf.h>
46#include <sys/un.h>
47#include <sys/unpcb.h>
48#define KERNEL
49struct uio;
50#include <sys/file.h>
51struct file *file, *fileNFILE;
52int nfiles;
53
54int Aflag;
55extern char *calloc();
56
57unixpr(fileheadaddr, nfilesaddr, unixsw)
58 off_t fileheadaddr, nfilesaddr;
59 struct protosw *unixsw;
60{
61 register struct file *fp, *lfp;
62 struct file *filep;
63 struct socket sock, *so = &sock;
64 int i;
65
66 if (fileheadaddr == 0 || nfilesaddr == 0) {
67 printf("filehead or nfiles not in namelist.\n");
68 return;
69 }
70 if (kvm_read(nfilesaddr, (char *)&nfiles, sizeof (nfiles)) !=
71 sizeof (nfiles)) {
72 printf("nfiles: bad read.\n");
73 return;
74 }
75 if (kvm_read(fileheadaddr, (char *)&filep, sizeof (filep))
76 != sizeof (filep)) {
77 printf("File table address, bad read.\n");
78 return;
79 }
80 file = (struct file *)calloc(nfiles, sizeof (struct file));
81 if (file == (struct file *)0) {
82 printf("Out of memory (file table).\n");
83 return;
84 }
85 for (lfp = file, i = nfiles;
86 filep != NULL && i-- > 0;
87 filep = lfp->f_filef, lfp++)
88 {
89 if(kvm_read((off_t)filep, (char *)lfp, sizeof (struct file))
90 != sizeof(struct file)) {
91 printf("File table read error.\n");
92 return;
93 }
94 }
95 fileNFILE = file + nfiles;
96 for (fp = file; fp < fileNFILE; fp++) {
97 if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
98 continue;
99 if (kvm_read((off_t)fp->f_data, (char *)so, sizeof (*so))
100 != sizeof (*so))
101 continue;
102 /* kludge */
103 if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
104 if (so->so_pcb)
105 unixdomainpr(so, fp->f_data);
106 }
107 free((char *)file);
108}
109
110static char *socktype[] =
111 { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
112
113unixdomainpr(so, soaddr)
114 register struct socket *so;
115 caddr_t soaddr;
116{
117 struct unpcb unpcb, *unp = &unpcb;
118 struct mbuf mbuf, *m;
119 struct sockaddr_un *sa;
120 static int first = 1;
121
122 if (kvm_read((off_t)so->so_pcb, (char *)unp, sizeof (*unp))
123 != sizeof (*unp))
124 return;
125 if (unp->unp_addr) {
126 m = &mbuf;
127 if (kvm_read((off_t)unp->unp_addr, (char *)m, sizeof (*m))
128 != sizeof (*m))
129 m = (struct mbuf *)0;
130 sa = (struct sockaddr_un *)(m->m_dat);
131 } else
132 m = (struct mbuf *)0;
133 if (first) {
134 printf("Active UNIX domain sockets\n");
135 printf(
136"%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
137 "Address", "Type", "Recv-Q", "Send-Q",
138 "Inode", "Conn", "Refs", "Nextref");
139 first = 0;
140 }
141 printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
142 soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
143 unp->unp_vnode, unp->unp_conn,
144 unp->unp_refs, unp->unp_nextref);
145 if (m)
146 printf(" %.*s", m->m_len - sizeof(sa->sun_family),
147 sa->sun_path);
148 putchar('\n');
149}