17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0f1702c5SYu Xiangning * Common Development and Distribution License (the "License"). 6*0f1702c5SYu Xiangning * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*0f1702c5SYu Xiangning * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 317c478bd9Sstevel@tonic-gate * The Regents of the University of California 327c478bd9Sstevel@tonic-gate * All Rights Reserved 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 357c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 367c478bd9Sstevel@tonic-gate * contributors. 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate /* 407c478bd9Sstevel@tonic-gate * code for netstat's -k option 417c478bd9Sstevel@tonic-gate * 427c478bd9Sstevel@tonic-gate * NOTES: 437c478bd9Sstevel@tonic-gate * 1. A comment "LINTED: (note 1)" appears before certain lines where 447c478bd9Sstevel@tonic-gate * lint would have complained, "pointer cast may result in improper 457c478bd9Sstevel@tonic-gate * alignment". These are lines where lint had suspected potential 467c478bd9Sstevel@tonic-gate * improper alignment of a data structure; in each such situation 477c478bd9Sstevel@tonic-gate * we have relied on the kernel guaranteeing proper alignment. 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate #include <stdio.h> 517c478bd9Sstevel@tonic-gate #include <stdlib.h> 527c478bd9Sstevel@tonic-gate #include <unistd.h> 537c478bd9Sstevel@tonic-gate #include <strings.h> 547c478bd9Sstevel@tonic-gate #include <string.h> 557c478bd9Sstevel@tonic-gate #include <kstat.h> 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #include <sys/types.h> 587c478bd9Sstevel@tonic-gate #include <sys/socket.h> 597c478bd9Sstevel@tonic-gate #include <sys/stream.h> 607c478bd9Sstevel@tonic-gate #include <sys/tiuser.h> 617c478bd9Sstevel@tonic-gate #include <sys/socketvar.h> 627c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static char *typetoname(t_scalar_t); 657c478bd9Sstevel@tonic-gate static void print_kn(kstat_t *); 667c478bd9Sstevel@tonic-gate static char *nextstr(char *); 677c478bd9Sstevel@tonic-gate extern void fail(int, char *, ...); 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate #define NALEN 8 /* nulladdress string length */ 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* 727c478bd9Sstevel@tonic-gate * Print a summary of connections related to a unix protocol. 737c478bd9Sstevel@tonic-gate */ 747c478bd9Sstevel@tonic-gate void 757c478bd9Sstevel@tonic-gate unixpr(kstat_ctl_t *kc) 767c478bd9Sstevel@tonic-gate { 777c478bd9Sstevel@tonic-gate kstat_t *ksp; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate if (kc == NULL) { /* sanity check. */ 807c478bd9Sstevel@tonic-gate fail(0, "unixpr: No kstat"); 817c478bd9Sstevel@tonic-gate exit(3); 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate /* find the sockfs kstat: */ 857c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "sockfs", 0, "sock_unix_list")) == 867c478bd9Sstevel@tonic-gate (kstat_t *)NULL) { 877c478bd9Sstevel@tonic-gate fail(0, "kstat_data_lookup failed\n"); 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, NULL) == -1) { 917c478bd9Sstevel@tonic-gate fail(0, "kstat_read failed for sock_unix_list\n"); 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate print_kn(ksp); 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate static void 987c478bd9Sstevel@tonic-gate print_kn(kstat_t *ksp) 997c478bd9Sstevel@tonic-gate { 1007c478bd9Sstevel@tonic-gate int i; 1017c478bd9Sstevel@tonic-gate struct sockinfo *psi; /* ptr to current sockinfo */ 1027c478bd9Sstevel@tonic-gate char *pas; /* ptr to string-format addrs */ 1037c478bd9Sstevel@tonic-gate char *nullstr; /* ptr to null string */ 1047c478bd9Sstevel@tonic-gate char *conn_vp; 1057c478bd9Sstevel@tonic-gate char *local_vp; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate if (ksp->ks_ndata == 0) { 1087c478bd9Sstevel@tonic-gate return; /* no AF_UNIX sockets found */ 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate /* 1127c478bd9Sstevel@tonic-gate * Having ks_data set with ks_data == NULL shouldn't happen; 1137c478bd9Sstevel@tonic-gate * If it does, the sockfs kstat is seriously broken. 1147c478bd9Sstevel@tonic-gate */ 1157c478bd9Sstevel@tonic-gate if ((psi = ksp->ks_data) == NULL) { 1167c478bd9Sstevel@tonic-gate fail(0, "print_kn: no kstat data\n"); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* set pas to the address strings which are after the sockinfo */ 1207c478bd9Sstevel@tonic-gate pas = &((char *)psi)[sizeof (struct sockinfo)]; 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate /* Create a string of NALEN "0"'s for NULL addresses. */ 1237c478bd9Sstevel@tonic-gate if ((nullstr = calloc(1, NALEN)) == NULL) { 1247c478bd9Sstevel@tonic-gate fail(0, "print_kn: out of memory\n"); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate (void) memset((void *)nullstr, '0', NALEN); 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate (void) printf("\nActive UNIX domain sockets\n"); 1297c478bd9Sstevel@tonic-gate (void) printf("%-8.8s %-10.10s %8.8s %8.8s " 1307c478bd9Sstevel@tonic-gate "Local Addr Remote Addr\n", 1317c478bd9Sstevel@tonic-gate "Address", "Type", "Vnode", "Conn"); 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate /* for each sockinfo structure, display what we need: */ 1347c478bd9Sstevel@tonic-gate for (i = 0; i < ksp->ks_ndata; i++) { 1357c478bd9Sstevel@tonic-gate /* display sonode's address. 1st string after sockinfo: */ 1367c478bd9Sstevel@tonic-gate pas = &(((char *)psi)[sizeof (struct sockinfo)]); 1377c478bd9Sstevel@tonic-gate (void) printf("%s ", pas); 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate (void) printf("%-10.10s ", typetoname(psi->si_serv_type)); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* laddr.sou_vp: 2nd string after sockinfo: */ 1427c478bd9Sstevel@tonic-gate pas = nextstr(pas); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate local_vp = conn_vp = nullstr; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate if ((psi->si_state & SS_ISBOUND) && 1477c478bd9Sstevel@tonic-gate (psi->si_ux_laddr_sou_magic == SOU_MAGIC_EXPLICIT)) { 1487c478bd9Sstevel@tonic-gate local_vp = pas; 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* faddr.sou_vp: 3rd string after sockinfo: */ 1527c478bd9Sstevel@tonic-gate pas = nextstr(pas); 1537c478bd9Sstevel@tonic-gate if ((psi->si_state & SS_ISCONNECTED) && 1547c478bd9Sstevel@tonic-gate (psi->si_ux_faddr_sou_magic == SOU_MAGIC_EXPLICIT)) { 1557c478bd9Sstevel@tonic-gate conn_vp = pas; 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate (void) printf("%s %s ", local_vp, conn_vp); 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate /* laddr.soa_sa: */ 1617c478bd9Sstevel@tonic-gate if ((psi->si_state & SS_ISBOUND) && 1627c478bd9Sstevel@tonic-gate strlen(psi->si_laddr_sun_path) != 0 && 1637c478bd9Sstevel@tonic-gate psi->si_laddr_soa_len != 0) { 164*0f1702c5SYu Xiangning if (psi->si_faddr_noxlate) { 1657c478bd9Sstevel@tonic-gate (void) printf(" (socketpair) "); 1667c478bd9Sstevel@tonic-gate } else { 1677c478bd9Sstevel@tonic-gate if (psi->si_laddr_soa_len > 1687c478bd9Sstevel@tonic-gate sizeof (psi->si_laddr_family)) 1697c478bd9Sstevel@tonic-gate (void) printf("%s ", 1707c478bd9Sstevel@tonic-gate psi->si_laddr_sun_path); 1717c478bd9Sstevel@tonic-gate else 1727c478bd9Sstevel@tonic-gate (void) printf(" "); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate } else 1757c478bd9Sstevel@tonic-gate (void) printf(" "); 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate /* faddr.soa_sa: */ 1787c478bd9Sstevel@tonic-gate if ((psi->si_state & SS_ISCONNECTED) && 1797c478bd9Sstevel@tonic-gate strlen(psi->si_faddr_sun_path) != 0 && 1807c478bd9Sstevel@tonic-gate psi->si_faddr_soa_len != 0) { 1817c478bd9Sstevel@tonic-gate 182*0f1702c5SYu Xiangning if (psi->si_faddr_noxlate) { 1837c478bd9Sstevel@tonic-gate (void) printf(" (socketpair) "); 1847c478bd9Sstevel@tonic-gate } else { 1857c478bd9Sstevel@tonic-gate if (psi->si_faddr_soa_len > 1867c478bd9Sstevel@tonic-gate sizeof (psi->si_faddr_family)) 1877c478bd9Sstevel@tonic-gate (void) printf("%s ", 1887c478bd9Sstevel@tonic-gate psi->si_faddr_sun_path); 1897c478bd9Sstevel@tonic-gate else 1907c478bd9Sstevel@tonic-gate (void) printf(" "); 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate } else 1937c478bd9Sstevel@tonic-gate (void) printf(" "); 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate (void) printf("\n"); 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* if si_size didn't get filled in, then we're done */ 1987c478bd9Sstevel@tonic-gate if (psi->si_size == 0 || 1997c478bd9Sstevel@tonic-gate !IS_P2ALIGNED(psi->si_size, sizeof (psi))) { 2007c478bd9Sstevel@tonic-gate break; 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate /* LINTED: (note 1) */ 2047c478bd9Sstevel@tonic-gate psi = (struct sockinfo *)&(((char *)psi)[psi->si_size]); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate static char * 2097c478bd9Sstevel@tonic-gate typetoname(t_scalar_t type) 2107c478bd9Sstevel@tonic-gate { 2117c478bd9Sstevel@tonic-gate switch (type) { 2127c478bd9Sstevel@tonic-gate case T_CLTS: 2137c478bd9Sstevel@tonic-gate return ("dgram"); 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate case T_COTS: 2167c478bd9Sstevel@tonic-gate return ("stream"); 2177c478bd9Sstevel@tonic-gate 2187c478bd9Sstevel@tonic-gate case T_COTS_ORD: 2197c478bd9Sstevel@tonic-gate return ("stream-ord"); 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate default: 2227c478bd9Sstevel@tonic-gate return (""); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate /* 2277c478bd9Sstevel@tonic-gate * nextstr(): find the beginning of a next string. 2287c478bd9Sstevel@tonic-gate * The sockfs kstat left-justifies each address string, leaving 2297c478bd9Sstevel@tonic-gate * null's between the strings. Since we don't necessarily know 2307c478bd9Sstevel@tonic-gate * the sizes of pointers in the kernel, we need to skip over these 2317c478bd9Sstevel@tonic-gate * nulls in order to get to the start of the next string. 2327c478bd9Sstevel@tonic-gate */ 2337c478bd9Sstevel@tonic-gate static char * 2347c478bd9Sstevel@tonic-gate nextstr(char *pas) 2357c478bd9Sstevel@tonic-gate { 2367c478bd9Sstevel@tonic-gate char *next; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate for (next = &pas[strlen(pas) + 1]; *next == NULL; ) { 2397c478bd9Sstevel@tonic-gate next++; 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate return (next); 2437c478bd9Sstevel@tonic-gate } 244