xref: /freebsd/usr.bin/netstat/unix.c (revision 5e0517186d418529323a5eb007287145e4fd3b50)
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 
349b50d902SRodney W. Grimes #ifndef lint
355d422d6aSPhilippe Charnier #if 0
369b50d902SRodney W. Grimes static char sccsid[] = "@(#)unix.c	8.1 (Berkeley) 6/6/93";
375d422d6aSPhilippe Charnier #endif
385d422d6aSPhilippe Charnier static const char rcsid[] =
39c3aac50fSPeter Wemm   "$FreeBSD$";
409b50d902SRodney W. Grimes #endif /* not lint */
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes /*
439b50d902SRodney W. Grimes  * Display protocol blocks in the unix domain.
449b50d902SRodney W. Grimes  */
459b50d902SRodney W. Grimes #include <sys/param.h>
46d8d89152SDavid Greenman #include <sys/queue.h>
479b50d902SRodney W. Grimes #include <sys/protosw.h>
489b50d902SRodney W. Grimes #include <sys/socket.h>
499b50d902SRodney W. Grimes #include <sys/socketvar.h>
509b50d902SRodney W. Grimes #include <sys/mbuf.h>
519b50d902SRodney W. Grimes #include <sys/sysctl.h>
529b50d902SRodney W. Grimes #include <sys/un.h>
539b50d902SRodney W. Grimes #include <sys/unpcb.h>
549b50d902SRodney W. Grimes 
559b50d902SRodney W. Grimes #include <netinet/in.h>
569b50d902SRodney W. Grimes 
574f81ef50SGarrett Wollman #include <errno.h>
58c6620a13SPoul-Henning Kamp #include <err.h>
598ad9b1a3SGarrett Wollman #include <stddef.h>
609b50d902SRodney W. Grimes #include <stdio.h>
619b50d902SRodney W. Grimes #include <stdlib.h>
624f81ef50SGarrett Wollman #include <kvm.h>
639b50d902SRodney W. Grimes #include "netstat.h"
649b50d902SRodney W. Grimes 
655e051718SAssar Westerlund static	void unixdomainpr (struct xunpcb *, struct xsocket *);
669b50d902SRodney W. Grimes 
674f81ef50SGarrett Wollman static	const char *const socktype[] =
689b50d902SRodney W. Grimes     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
699b50d902SRodney W. Grimes 
704f81ef50SGarrett Wollman void
715e051718SAssar Westerlund unixpr(void)
729b50d902SRodney W. Grimes {
734f81ef50SGarrett Wollman 	char 	*buf;
744f81ef50SGarrett Wollman 	int	type;
754f81ef50SGarrett Wollman 	size_t	len;
764f81ef50SGarrett Wollman 	struct	xsocket *so;
774f81ef50SGarrett Wollman 	struct	xunpgen *xug, *oxug;
784f81ef50SGarrett Wollman 	struct	xunpcb *xunp;
794f81ef50SGarrett Wollman 	char mibvar[sizeof "net.local.seqpacket.pcblist"];
804f81ef50SGarrett Wollman 
81b13864ecSBill Fenner 	for (type = SOCK_STREAM; type <= SOCK_SEQPACKET; type++) {
824f81ef50SGarrett Wollman 		sprintf(mibvar, "net.local.%s.pcblist", socktype[type]);
834f81ef50SGarrett Wollman 
844f81ef50SGarrett Wollman 		len = 0;
854f81ef50SGarrett Wollman 		if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
864f81ef50SGarrett Wollman 			if (errno != ENOENT)
874f81ef50SGarrett Wollman 				warn("sysctl: %s", mibvar);
884f81ef50SGarrett Wollman 			continue;
894f81ef50SGarrett Wollman 		}
904f81ef50SGarrett Wollman 		if ((buf = malloc(len)) == 0) {
914f81ef50SGarrett Wollman 			warn("malloc %lu bytes", (u_long)len);
924f81ef50SGarrett Wollman 			return;
934f81ef50SGarrett Wollman 		}
944f81ef50SGarrett Wollman 		if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
954f81ef50SGarrett Wollman 			warn("sysctl: %s", mibvar);
964f81ef50SGarrett Wollman 			free(buf);
974f81ef50SGarrett Wollman 			return;
984f81ef50SGarrett Wollman 		}
994f81ef50SGarrett Wollman 
1004f81ef50SGarrett Wollman 		oxug = xug = (struct xunpgen *)buf;
1014f81ef50SGarrett Wollman 		for (xug = (struct xunpgen *)((char *)xug + xug->xug_len);
1024f81ef50SGarrett Wollman 		     xug->xug_len > sizeof(struct xunpgen);
1034f81ef50SGarrett Wollman 		     xug = (struct xunpgen *)((char *)xug + xug->xug_len)) {
1044f81ef50SGarrett Wollman 			xunp = (struct xunpcb *)xug;
1054f81ef50SGarrett Wollman 			so = &xunp->xu_socket;
1064f81ef50SGarrett Wollman 
1074f81ef50SGarrett Wollman 			/* Ignore PCBs which were freed during copyout. */
1084f81ef50SGarrett Wollman 			if (xunp->xu_unp.unp_gencnt > oxug->xug_gen)
1094f81ef50SGarrett Wollman 				continue;
1104f81ef50SGarrett Wollman 			unixdomainpr(xunp, so);
1114f81ef50SGarrett Wollman 		}
1124f81ef50SGarrett Wollman 		if (xug != oxug && xug->xug_gen != oxug->xug_gen) {
1134f81ef50SGarrett Wollman 			if (oxug->xug_count > xug->xug_count) {
1144f81ef50SGarrett Wollman 				printf("Some %s sockets may have been deleted.\n",
1154f81ef50SGarrett Wollman 				       socktype[type]);
1164f81ef50SGarrett Wollman 			} else if (oxug->xug_count < xug->xug_count) {
1174f81ef50SGarrett Wollman 				printf("Some %s sockets may have been created.\n",
1184f81ef50SGarrett Wollman 			       socktype[type]);
1194f81ef50SGarrett Wollman 			} else {
1204f81ef50SGarrett Wollman 				printf("Some %s sockets may have been created or deleted",
1214f81ef50SGarrett Wollman 			       socktype[type]);
1224f81ef50SGarrett Wollman 			}
1234f81ef50SGarrett Wollman 		}
1244f81ef50SGarrett Wollman 		free(buf);
1254f81ef50SGarrett Wollman 	}
1264f81ef50SGarrett Wollman }
1274f81ef50SGarrett Wollman 
1284f81ef50SGarrett Wollman static void
1295e051718SAssar Westerlund unixdomainpr(struct xunpcb *xunp, struct xsocket *so)
1304f81ef50SGarrett Wollman {
1314f81ef50SGarrett Wollman 	struct unpcb *unp;
1324f81ef50SGarrett Wollman 	struct sockaddr_un *sa;
1339b50d902SRodney W. Grimes 	static int first = 1;
1349b50d902SRodney W. Grimes 
1354f81ef50SGarrett Wollman 	unp = &xunp->xu_unp;
1364f81ef50SGarrett Wollman 	if (unp->unp_addr)
1374f81ef50SGarrett Wollman 		sa = &xunp->xu_addr;
1384f81ef50SGarrett Wollman 	else
139e4bb0b9aSGarrett Wollman 		sa = (struct sockaddr_un *)0;
1404f81ef50SGarrett Wollman 
1419b50d902SRodney W. Grimes 	if (first) {
1429b50d902SRodney W. Grimes 		printf("Active UNIX domain sockets\n");
1439b50d902SRodney W. Grimes 		printf(
1449b50d902SRodney W. Grimes "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
1459b50d902SRodney W. Grimes 		    "Address", "Type", "Recv-Q", "Send-Q",
1469b50d902SRodney W. Grimes 		    "Inode", "Conn", "Refs", "Nextref");
1479b50d902SRodney W. Grimes 		first = 0;
1489b50d902SRodney W. Grimes 	}
1494f81ef50SGarrett Wollman 	printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
1504f81ef50SGarrett Wollman 	       (long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc,
1514f81ef50SGarrett Wollman 	       so->so_snd.sb_cc,
1524f81ef50SGarrett Wollman 	       (long)unp->unp_vnode, (long)unp->unp_conn,
1534d51ef63SPoul-Henning Kamp 	       (long)LIST_FIRST(&unp->unp_refs), (long)LIST_NEXT(unp, unp_reflink));
154e4bb0b9aSGarrett Wollman 	if (sa)
155bc6ee716SAndrey A. Chernov 		printf(" %.*s",
15622694ebaSBruce Evans 		    (int)(sa->sun_len - offsetof(struct sockaddr_un, sun_path)),
157bc6ee716SAndrey A. Chernov 		    sa->sun_path);
1589b50d902SRodney W. Grimes 	putchar('\n');
1599b50d902SRodney W. Grimes }
160