Annotation of src/usr.bin/netstat/unix.c, Revision 1.32
1.32 ! guenther 1: /* $OpenBSD: unix.c,v 1.31 2018/01/05 10:41:24 mpi Exp $ */
1.1 deraadt 2: /* $NetBSD: unix.c,v 1.13 1995/10/03 21:42:48 thorpej Exp $ */
3:
4: /*-
5: * Copyright (c) 1983, 1988, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.10 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: /*
34: * Display protocol blocks in the unix domain.
35: */
36: #include <sys/socket.h>
1.25 deraadt 37: #include <sys/protosw.h>
1.1 deraadt 38: #include <sys/mbuf.h>
39: #include <sys/sysctl.h>
1.29 guenther 40: #include <sys/time.h>
1.1 deraadt 41: #include <sys/un.h>
42: #include <sys/unpcb.h>
1.23 deraadt 43: #include <sys/ucred.h>
1.1 deraadt 44:
45: #include <netinet/in.h>
46:
1.5 millert 47: #include <limits.h>
1.1 deraadt 48: #include <stdio.h>
49: #include <stdlib.h>
50: #include <kvm.h>
51: #include "netstat.h"
52:
1.21 guenther 53: static const char *socktype[] =
1.1 deraadt 54: { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
55:
1.26 claudio 56: void
57: unixdomainpr(struct kinfo_file *kf)
1.1 deraadt 58: {
59: static int first = 1;
60:
1.26 claudio 61: /* XXX should fix kinfo_file instead but not now */
62: if (kf->so_pcb == -1)
63: kf->so_pcb = 0;
1.15 bluhm 64:
1.1 deraadt 65: if (first) {
66: printf("Active UNIX domain sockets\n");
1.11 deraadt 67: printf("%-*.*s %-6.6s %-6.6s %-6.6s %*.*s %*.*s %*.*s %*.*s Addr\n",
1.5 millert 68: PLEN, PLEN, "Address", "Type", "Recv-Q", "Send-Q",
69: PLEN, PLEN, "Inode", PLEN, PLEN, "Conn",
70: PLEN, PLEN, "Refs", PLEN, PLEN, "Nextref");
1.1 deraadt 71: first = 0;
72: }
1.21 guenther 73:
1.22 guenther 74: printf("%#*llx%s %-6.6s %6llu %6llu %#*llx%s %#*llx%s %#*llx%s %#*llx%s",
1.26 claudio 75: FAKE_PTR(kf->so_pcb), socktype[kf->so_type],
1.21 guenther 76: kf->so_rcv_cc, kf->so_snd_cc,
77: FAKE_PTR(kf->v_un),
78: FAKE_PTR(kf->unp_conn),
79: FAKE_PTR(kf->unp_refs),
80: FAKE_PTR(kf->unp_nextref));
81: if (kf->unp_path[0] != '\0')
82: printf(" %.*s", KI_UNPPATHLEN, kf->unp_path);
1.1 deraadt 83: putchar('\n');
1.15 bluhm 84: }
85:
86: /*
87: * Dump the contents of a UNIX PCB
88: */
89: void
90: unpcb_dump(u_long off)
91: {
92: struct unpcb unp;
93:
94: if (off == 0)
95: return;
96: kread(off, &unp, sizeof(unp));
97:
1.26 claudio 98: if (vflag)
99: socket_dump((u_long)unp.unp_socket);
100:
1.18 deraadt 101: #define p(fmt, v, sep) printf(#v " " fmt sep, unp.v);
102: #define pll(fmt, v, sep) printf(#v " " fmt sep, (long long) unp.v);
1.19 deraadt 103: #define pull(fmt, v, sep) printf(#v " " fmt sep, (unsigned long long) unp.v);
1.26 claudio 104: #define pp(fmt, v, sep) printf(#v " " fmt sep, unp.v);
105: printf("unpcb %#lx\n ", off);
1.16 deraadt 106: pp("%p", unp_socket, "\n ");
107: pp("%p", unp_vnode, ", ");
1.19 deraadt 108: pull("%llu", unp_ino, "\n ");
1.16 deraadt 109: pp("%p", unp_conn, ", ");
1.27 bluhm 110: printf("unp_refs %p, ", SLIST_FIRST(&unp.unp_refs));
111: printf("unp_nextref %p\n ", SLIST_NEXT(&unp, unp_nextref));
1.16 deraadt 112: pp("%p", unp_addr, "\n ");
1.20 guenther 113: p("%#.8x", unp_flags, "\n ");
1.18 deraadt 114: p("%u", unp_connid.uid, ", ");
115: p("%u", unp_connid.gid, ", ");
116: p("%d", unp_connid.pid, "\n ");
117: pll("%lld", unp_ctime.tv_sec, ", ");
118: p("%ld", unp_ctime.tv_nsec, "\n");
1.15 bluhm 119: #undef p
1.16 deraadt 120: #undef pp
1.1 deraadt 121: }