19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1983, 1988, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 345d422d6aSPhilippe Charnier #if 0 356cc6f122SPhilippe Charnier #ifndef lint 369b50d902SRodney W. Grimes static char sccsid[] = "@(#)unix.c 8.1 (Berkeley) 6/6/93"; 379b50d902SRodney W. Grimes #endif /* not lint */ 386cc6f122SPhilippe Charnier #endif 396cc6f122SPhilippe Charnier 406cc6f122SPhilippe Charnier #include <sys/cdefs.h> 416cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$"); 429b50d902SRodney W. Grimes 439b50d902SRodney W. Grimes /* 449b50d902SRodney W. Grimes * Display protocol blocks in the unix domain. 459b50d902SRodney W. Grimes */ 469b50d902SRodney W. Grimes #include <sys/param.h> 47d8d89152SDavid Greenman #include <sys/queue.h> 489b50d902SRodney W. Grimes #include <sys/protosw.h> 499b50d902SRodney W. Grimes #include <sys/socket.h> 509b50d902SRodney W. Grimes #include <sys/socketvar.h> 519b50d902SRodney W. Grimes #include <sys/mbuf.h> 529b50d902SRodney W. Grimes #include <sys/sysctl.h> 539b50d902SRodney W. Grimes #include <sys/un.h> 549b50d902SRodney W. Grimes #include <sys/unpcb.h> 559b50d902SRodney W. Grimes 569b50d902SRodney W. Grimes #include <netinet/in.h> 579b50d902SRodney W. Grimes 584f81ef50SGarrett Wollman #include <errno.h> 59c6620a13SPoul-Henning Kamp #include <err.h> 608ad9b1a3SGarrett Wollman #include <stddef.h> 617b95a1ebSYaroslav Tykhiy #include <stdint.h> 629b50d902SRodney W. Grimes #include <stdio.h> 639b50d902SRodney W. Grimes #include <stdlib.h> 644f81ef50SGarrett Wollman #include <kvm.h> 659b50d902SRodney W. Grimes #include "netstat.h" 669b50d902SRodney W. Grimes 675e051718SAssar Westerlund static void unixdomainpr (struct xunpcb *, struct xsocket *); 689b50d902SRodney W. Grimes 694f81ef50SGarrett Wollman static const char *const socktype[] = 709b50d902SRodney W. Grimes { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" }; 719b50d902SRodney W. Grimes 724f81ef50SGarrett Wollman void 735e051718SAssar Westerlund unixpr(void) 749b50d902SRodney W. Grimes { 754f81ef50SGarrett Wollman char *buf; 764f81ef50SGarrett Wollman int type; 774f81ef50SGarrett Wollman size_t len; 784f81ef50SGarrett Wollman struct xsocket *so; 794f81ef50SGarrett Wollman struct xunpgen *xug, *oxug; 804f81ef50SGarrett Wollman struct xunpcb *xunp; 814f81ef50SGarrett Wollman char mibvar[sizeof "net.local.seqpacket.pcblist"]; 824f81ef50SGarrett Wollman 83b13864ecSBill Fenner for (type = SOCK_STREAM; type <= SOCK_SEQPACKET; type++) { 844f81ef50SGarrett Wollman sprintf(mibvar, "net.local.%s.pcblist", socktype[type]); 854f81ef50SGarrett Wollman 864f81ef50SGarrett Wollman len = 0; 874f81ef50SGarrett Wollman if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) { 884f81ef50SGarrett Wollman if (errno != ENOENT) 894f81ef50SGarrett Wollman warn("sysctl: %s", mibvar); 904f81ef50SGarrett Wollman continue; 914f81ef50SGarrett Wollman } 924f81ef50SGarrett Wollman if ((buf = malloc(len)) == 0) { 936cc6f122SPhilippe Charnier warnx("malloc %lu bytes", (u_long)len); 944f81ef50SGarrett Wollman return; 954f81ef50SGarrett Wollman } 964f81ef50SGarrett Wollman if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) { 974f81ef50SGarrett Wollman warn("sysctl: %s", mibvar); 984f81ef50SGarrett Wollman free(buf); 994f81ef50SGarrett Wollman return; 1004f81ef50SGarrett Wollman } 1014f81ef50SGarrett Wollman 1024f81ef50SGarrett Wollman oxug = xug = (struct xunpgen *)buf; 1034f81ef50SGarrett Wollman for (xug = (struct xunpgen *)((char *)xug + xug->xug_len); 1044f81ef50SGarrett Wollman xug->xug_len > sizeof(struct xunpgen); 1054f81ef50SGarrett Wollman xug = (struct xunpgen *)((char *)xug + xug->xug_len)) { 1064f81ef50SGarrett Wollman xunp = (struct xunpcb *)xug; 1074f81ef50SGarrett Wollman so = &xunp->xu_socket; 1084f81ef50SGarrett Wollman 1094f81ef50SGarrett Wollman /* Ignore PCBs which were freed during copyout. */ 1104f81ef50SGarrett Wollman if (xunp->xu_unp.unp_gencnt > oxug->xug_gen) 1114f81ef50SGarrett Wollman continue; 1124f81ef50SGarrett Wollman unixdomainpr(xunp, so); 1134f81ef50SGarrett Wollman } 1144f81ef50SGarrett Wollman if (xug != oxug && xug->xug_gen != oxug->xug_gen) { 1154f81ef50SGarrett Wollman if (oxug->xug_count > xug->xug_count) { 1164f81ef50SGarrett Wollman printf("Some %s sockets may have been deleted.\n", 1174f81ef50SGarrett Wollman socktype[type]); 1184f81ef50SGarrett Wollman } else if (oxug->xug_count < xug->xug_count) { 1194f81ef50SGarrett Wollman printf("Some %s sockets may have been created.\n", 1204f81ef50SGarrett Wollman socktype[type]); 1214f81ef50SGarrett Wollman } else { 1224f81ef50SGarrett Wollman printf("Some %s sockets may have been created or deleted", 1234f81ef50SGarrett Wollman socktype[type]); 1244f81ef50SGarrett Wollman } 1254f81ef50SGarrett Wollman } 1264f81ef50SGarrett Wollman free(buf); 1274f81ef50SGarrett Wollman } 1284f81ef50SGarrett Wollman } 1294f81ef50SGarrett Wollman 1304f81ef50SGarrett Wollman static void 1315e051718SAssar Westerlund unixdomainpr(struct xunpcb *xunp, struct xsocket *so) 1324f81ef50SGarrett Wollman { 1334f81ef50SGarrett Wollman struct unpcb *unp; 1344f81ef50SGarrett Wollman struct sockaddr_un *sa; 1359b50d902SRodney W. Grimes static int first = 1; 1369b50d902SRodney W. Grimes 1374f81ef50SGarrett Wollman unp = &xunp->xu_unp; 1384f81ef50SGarrett Wollman if (unp->unp_addr) 1394f81ef50SGarrett Wollman sa = &xunp->xu_addr; 1404f81ef50SGarrett Wollman else 141e4bb0b9aSGarrett Wollman sa = (struct sockaddr_un *)0; 1424f81ef50SGarrett Wollman 1439b50d902SRodney W. Grimes if (first) { 1449b50d902SRodney W. Grimes printf("Active UNIX domain sockets\n"); 1459b50d902SRodney W. Grimes printf( 1469b50d902SRodney W. Grimes "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n", 1479b50d902SRodney W. Grimes "Address", "Type", "Recv-Q", "Send-Q", 1489b50d902SRodney W. Grimes "Inode", "Conn", "Refs", "Nextref"); 1499b50d902SRodney W. Grimes first = 0; 1509b50d902SRodney W. Grimes } 151f824b518SJohn Polstra printf("%8lx %-6.6s %6u %6u %8lx %8lx %8lx %8lx", 1524f81ef50SGarrett Wollman (long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc, 1534f81ef50SGarrett Wollman so->so_snd.sb_cc, 1544f81ef50SGarrett Wollman (long)unp->unp_vnode, (long)unp->unp_conn, 1554d51ef63SPoul-Henning Kamp (long)LIST_FIRST(&unp->unp_refs), (long)LIST_NEXT(unp, unp_reflink)); 156e4bb0b9aSGarrett Wollman if (sa) 157bc6ee716SAndrey A. Chernov printf(" %.*s", 15822694ebaSBruce Evans (int)(sa->sun_len - offsetof(struct sockaddr_un, sun_path)), 159bc6ee716SAndrey A. Chernov sa->sun_path); 1609b50d902SRodney W. Grimes putchar('\n'); 1619b50d902SRodney W. Grimes } 162