10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*8348SEric.Yu@Sun.COM * Common Development and Distribution License (the "License"). 6*8348SEric.Yu@Sun.COM * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*8348SEric.Yu@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 270Sstevel@tonic-gate /* All Rights Reserved */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 310Sstevel@tonic-gate * The Regents of the University of California 320Sstevel@tonic-gate * All Rights Reserved 330Sstevel@tonic-gate * 340Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 350Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 360Sstevel@tonic-gate * contributors. 370Sstevel@tonic-gate */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate /* 400Sstevel@tonic-gate * code for netstat's -k option 410Sstevel@tonic-gate * 420Sstevel@tonic-gate * NOTES: 430Sstevel@tonic-gate * 1. A comment "LINTED: (note 1)" appears before certain lines where 440Sstevel@tonic-gate * lint would have complained, "pointer cast may result in improper 450Sstevel@tonic-gate * alignment". These are lines where lint had suspected potential 460Sstevel@tonic-gate * improper alignment of a data structure; in each such situation 470Sstevel@tonic-gate * we have relied on the kernel guaranteeing proper alignment. 480Sstevel@tonic-gate */ 490Sstevel@tonic-gate 500Sstevel@tonic-gate #include <stdio.h> 510Sstevel@tonic-gate #include <stdlib.h> 520Sstevel@tonic-gate #include <unistd.h> 530Sstevel@tonic-gate #include <strings.h> 540Sstevel@tonic-gate #include <string.h> 550Sstevel@tonic-gate #include <kstat.h> 560Sstevel@tonic-gate 570Sstevel@tonic-gate #include <sys/types.h> 580Sstevel@tonic-gate #include <sys/socket.h> 590Sstevel@tonic-gate #include <sys/stream.h> 600Sstevel@tonic-gate #include <sys/tiuser.h> 610Sstevel@tonic-gate #include <sys/socketvar.h> 620Sstevel@tonic-gate #include <sys/sysmacros.h> 630Sstevel@tonic-gate 640Sstevel@tonic-gate static char *typetoname(t_scalar_t); 650Sstevel@tonic-gate static void print_kn(kstat_t *); 660Sstevel@tonic-gate static char *nextstr(char *); 670Sstevel@tonic-gate extern void fail(int, char *, ...); 680Sstevel@tonic-gate 690Sstevel@tonic-gate #define NALEN 8 /* nulladdress string length */ 700Sstevel@tonic-gate 710Sstevel@tonic-gate /* 720Sstevel@tonic-gate * Print a summary of connections related to a unix protocol. 730Sstevel@tonic-gate */ 740Sstevel@tonic-gate void 750Sstevel@tonic-gate unixpr(kstat_ctl_t *kc) 760Sstevel@tonic-gate { 770Sstevel@tonic-gate kstat_t *ksp; 780Sstevel@tonic-gate 790Sstevel@tonic-gate if (kc == NULL) { /* sanity check. */ 800Sstevel@tonic-gate fail(0, "unixpr: No kstat"); 810Sstevel@tonic-gate exit(3); 820Sstevel@tonic-gate } 830Sstevel@tonic-gate 840Sstevel@tonic-gate /* find the sockfs kstat: */ 850Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "sockfs", 0, "sock_unix_list")) == 860Sstevel@tonic-gate (kstat_t *)NULL) { 870Sstevel@tonic-gate fail(0, "kstat_data_lookup failed\n"); 880Sstevel@tonic-gate } 890Sstevel@tonic-gate 900Sstevel@tonic-gate if (kstat_read(kc, ksp, NULL) == -1) { 910Sstevel@tonic-gate fail(0, "kstat_read failed for sock_unix_list\n"); 920Sstevel@tonic-gate } 930Sstevel@tonic-gate 940Sstevel@tonic-gate print_kn(ksp); 950Sstevel@tonic-gate } 960Sstevel@tonic-gate 970Sstevel@tonic-gate static void 980Sstevel@tonic-gate print_kn(kstat_t *ksp) 990Sstevel@tonic-gate { 1000Sstevel@tonic-gate int i; 1010Sstevel@tonic-gate struct sockinfo *psi; /* ptr to current sockinfo */ 1020Sstevel@tonic-gate char *pas; /* ptr to string-format addrs */ 1030Sstevel@tonic-gate char *nullstr; /* ptr to null string */ 1040Sstevel@tonic-gate char *conn_vp; 1050Sstevel@tonic-gate char *local_vp; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate if (ksp->ks_ndata == 0) { 1080Sstevel@tonic-gate return; /* no AF_UNIX sockets found */ 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* 1120Sstevel@tonic-gate * Having ks_data set with ks_data == NULL shouldn't happen; 1130Sstevel@tonic-gate * If it does, the sockfs kstat is seriously broken. 1140Sstevel@tonic-gate */ 1150Sstevel@tonic-gate if ((psi = ksp->ks_data) == NULL) { 1160Sstevel@tonic-gate fail(0, "print_kn: no kstat data\n"); 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* set pas to the address strings which are after the sockinfo */ 1200Sstevel@tonic-gate pas = &((char *)psi)[sizeof (struct sockinfo)]; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate /* Create a string of NALEN "0"'s for NULL addresses. */ 1230Sstevel@tonic-gate if ((nullstr = calloc(1, NALEN)) == NULL) { 1240Sstevel@tonic-gate fail(0, "print_kn: out of memory\n"); 1250Sstevel@tonic-gate } 1260Sstevel@tonic-gate (void) memset((void *)nullstr, '0', NALEN); 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate (void) printf("\nActive UNIX domain sockets\n"); 1290Sstevel@tonic-gate (void) printf("%-8.8s %-10.10s %8.8s %8.8s " 130*8348SEric.Yu@Sun.COM "Local Addr Remote Addr\n", 131*8348SEric.Yu@Sun.COM "Address", "Type", "Vnode", "Conn"); 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate /* for each sockinfo structure, display what we need: */ 1340Sstevel@tonic-gate for (i = 0; i < ksp->ks_ndata; i++) { 1350Sstevel@tonic-gate /* display sonode's address. 1st string after sockinfo: */ 1360Sstevel@tonic-gate pas = &(((char *)psi)[sizeof (struct sockinfo)]); 1370Sstevel@tonic-gate (void) printf("%s ", pas); 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate (void) printf("%-10.10s ", typetoname(psi->si_serv_type)); 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate /* laddr.sou_vp: 2nd string after sockinfo: */ 1420Sstevel@tonic-gate pas = nextstr(pas); 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate local_vp = conn_vp = nullstr; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate if ((psi->si_state & SS_ISBOUND) && 1470Sstevel@tonic-gate (psi->si_ux_laddr_sou_magic == SOU_MAGIC_EXPLICIT)) { 1480Sstevel@tonic-gate local_vp = pas; 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate /* faddr.sou_vp: 3rd string after sockinfo: */ 1520Sstevel@tonic-gate pas = nextstr(pas); 1530Sstevel@tonic-gate if ((psi->si_state & SS_ISCONNECTED) && 1540Sstevel@tonic-gate (psi->si_ux_faddr_sou_magic == SOU_MAGIC_EXPLICIT)) { 1550Sstevel@tonic-gate conn_vp = pas; 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate (void) printf("%s %s ", local_vp, conn_vp); 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate /* laddr.soa_sa: */ 1610Sstevel@tonic-gate if ((psi->si_state & SS_ISBOUND) && 1620Sstevel@tonic-gate strlen(psi->si_laddr_sun_path) != 0 && 1630Sstevel@tonic-gate psi->si_laddr_soa_len != 0) { 164*8348SEric.Yu@Sun.COM if (psi->si_faddr_noxlate) { 1650Sstevel@tonic-gate (void) printf(" (socketpair) "); 1660Sstevel@tonic-gate } else { 1670Sstevel@tonic-gate if (psi->si_laddr_soa_len > 168*8348SEric.Yu@Sun.COM sizeof (psi->si_laddr_family)) 1690Sstevel@tonic-gate (void) printf("%s ", 170*8348SEric.Yu@Sun.COM psi->si_laddr_sun_path); 1710Sstevel@tonic-gate else 1720Sstevel@tonic-gate (void) printf(" "); 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate } else 1750Sstevel@tonic-gate (void) printf(" "); 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate /* faddr.soa_sa: */ 1780Sstevel@tonic-gate if ((psi->si_state & SS_ISCONNECTED) && 1790Sstevel@tonic-gate strlen(psi->si_faddr_sun_path) != 0 && 1800Sstevel@tonic-gate psi->si_faddr_soa_len != 0) { 1810Sstevel@tonic-gate 182*8348SEric.Yu@Sun.COM if (psi->si_faddr_noxlate) { 1830Sstevel@tonic-gate (void) printf(" (socketpair) "); 1840Sstevel@tonic-gate } else { 1850Sstevel@tonic-gate if (psi->si_faddr_soa_len > 186*8348SEric.Yu@Sun.COM sizeof (psi->si_faddr_family)) 1870Sstevel@tonic-gate (void) printf("%s ", 188*8348SEric.Yu@Sun.COM psi->si_faddr_sun_path); 1890Sstevel@tonic-gate else 1900Sstevel@tonic-gate (void) printf(" "); 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate } else 1930Sstevel@tonic-gate (void) printf(" "); 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate (void) printf("\n"); 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate /* if si_size didn't get filled in, then we're done */ 1980Sstevel@tonic-gate if (psi->si_size == 0 || 1990Sstevel@tonic-gate !IS_P2ALIGNED(psi->si_size, sizeof (psi))) { 2000Sstevel@tonic-gate break; 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate /* LINTED: (note 1) */ 2040Sstevel@tonic-gate psi = (struct sockinfo *)&(((char *)psi)[psi->si_size]); 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate static char * 2090Sstevel@tonic-gate typetoname(t_scalar_t type) 2100Sstevel@tonic-gate { 2110Sstevel@tonic-gate switch (type) { 2120Sstevel@tonic-gate case T_CLTS: 2130Sstevel@tonic-gate return ("dgram"); 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate case T_COTS: 2160Sstevel@tonic-gate return ("stream"); 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate case T_COTS_ORD: 2190Sstevel@tonic-gate return ("stream-ord"); 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate default: 2220Sstevel@tonic-gate return (""); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate /* 2270Sstevel@tonic-gate * nextstr(): find the beginning of a next string. 2280Sstevel@tonic-gate * The sockfs kstat left-justifies each address string, leaving 2290Sstevel@tonic-gate * null's between the strings. Since we don't necessarily know 2300Sstevel@tonic-gate * the sizes of pointers in the kernel, we need to skip over these 2310Sstevel@tonic-gate * nulls in order to get to the start of the next string. 2320Sstevel@tonic-gate */ 2330Sstevel@tonic-gate static char * 2340Sstevel@tonic-gate nextstr(char *pas) 2350Sstevel@tonic-gate { 2360Sstevel@tonic-gate char *next; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate for (next = &pas[strlen(pas) + 1]; *next == NULL; ) { 2390Sstevel@tonic-gate next++; 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate return (next); 2430Sstevel@tonic-gate } 244