dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.bin / netstat / unix.c
blobf6b7e0e76f6512e58e631ec30eddf02cdf2536c4
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * University Copyright- Copyright (c) 1982, 1986, 1988
31 * The Regents of the University of California
32 * All Rights Reserved
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
36 * contributors.
40 * code for netstat's -k option
42 * NOTES:
43 * 1. A comment "LINTED: (note 1)" appears before certain lines where
44 * lint would have complained, "pointer cast may result in improper
45 * alignment". These are lines where lint had suspected potential
46 * improper alignment of a data structure; in each such situation
47 * we have relied on the kernel guaranteeing proper alignment.
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <strings.h>
54 #include <string.h>
55 #include <kstat.h>
57 #include <sys/types.h>
58 #include <sys/socket.h>
59 #include <sys/stream.h>
60 #include <sys/tiuser.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysmacros.h>
64 static char *typetoname(t_scalar_t);
65 static void print_kn(kstat_t *);
66 static char *nextstr(char *);
67 extern void fail(int, char *, ...);
69 #define NALEN 8 /* nulladdress string length */
72 * Print a summary of connections related to a unix protocol.
74 void
75 unixpr(kstat_ctl_t *kc)
77 kstat_t *ksp;
79 if (kc == NULL) { /* sanity check. */
80 fail(0, "unixpr: No kstat");
81 exit(3);
84 /* find the sockfs kstat: */
85 if ((ksp = kstat_lookup(kc, "sockfs", 0, "sock_unix_list")) ==
86 (kstat_t *)NULL) {
87 fail(0, "kstat_data_lookup failed\n");
90 if (kstat_read(kc, ksp, NULL) == -1) {
91 fail(0, "kstat_read failed for sock_unix_list\n");
94 print_kn(ksp);
97 static void
98 print_kn(kstat_t *ksp)
100 char nullstr[NALEN + 1];
101 int i;
102 struct sockinfo *psi; /* ptr to current sockinfo */
103 char *pas; /* ptr to string-format addrs */
104 const char *conn_vp;
105 const char *local_vp;
107 if (ksp->ks_ndata == 0) {
108 return; /* no AF_UNIX sockets found */
112 * Having ks_data set with ks_data == NULL shouldn't happen;
113 * If it does, the sockfs kstat is seriously broken.
115 if ((psi = ksp->ks_data) == NULL) {
116 fail(0, "print_kn: no kstat data\n");
119 /* set pas to the address strings which are after the sockinfo */
120 pas = &((char *)psi)[sizeof (struct sockinfo)];
122 /* Create a string of NALEN "0"'s for NULL addresses. */
123 memset(nullstr, '0', NALEN);
124 nullstr[NALEN] = '\0';
126 (void) printf("\nActive UNIX domain sockets\n");
127 (void) printf("%-8.8s %-10.10s %8.8s %8.8s "
128 "Local Addr Remote Addr\n",
129 "Address", "Type", "Vnode", "Conn");
131 /* for each sockinfo structure, display what we need: */
132 for (i = 0; i < ksp->ks_ndata; i++) {
133 /* display sonode's address. 1st string after sockinfo: */
134 pas = &(((char *)psi)[sizeof (struct sockinfo)]);
135 (void) printf("%s ", pas);
137 (void) printf("%-10.10s ", typetoname(psi->si_serv_type));
139 /* laddr.sou_vp: 2nd string after sockinfo: */
140 pas = nextstr(pas);
142 local_vp = conn_vp = nullstr;
144 if ((psi->si_state & SS_ISBOUND) &&
145 (psi->si_ux_laddr_sou_magic == SOU_MAGIC_EXPLICIT)) {
146 local_vp = pas;
149 /* faddr.sou_vp: 3rd string after sockinfo: */
150 pas = nextstr(pas);
151 if ((psi->si_state & SS_ISCONNECTED) &&
152 (psi->si_ux_faddr_sou_magic == SOU_MAGIC_EXPLICIT)) {
153 conn_vp = pas;
156 (void) printf("%s %s ", local_vp, conn_vp);
158 /* laddr.soa_sa: */
159 if ((psi->si_state & SS_ISBOUND) &&
160 strlen(psi->si_laddr_sun_path) != 0 &&
161 psi->si_laddr_soa_len != 0) {
162 if (psi->si_faddr_noxlate) {
163 (void) printf(" (socketpair) ");
164 } else {
165 if (psi->si_laddr_soa_len >
166 sizeof (psi->si_laddr_family))
167 (void) printf("%s ",
168 psi->si_laddr_sun_path);
169 else
170 (void) printf(" ");
172 } else
173 (void) printf(" ");
175 /* faddr.soa_sa: */
176 if ((psi->si_state & SS_ISCONNECTED) &&
177 strlen(psi->si_faddr_sun_path) != 0 &&
178 psi->si_faddr_soa_len != 0) {
180 if (psi->si_faddr_noxlate) {
181 (void) printf(" (socketpair) ");
182 } else {
183 if (psi->si_faddr_soa_len >
184 sizeof (psi->si_faddr_family))
185 (void) printf("%s ",
186 psi->si_faddr_sun_path);
187 else
188 (void) printf(" ");
190 } else
191 (void) printf(" ");
193 (void) printf("\n");
195 /* if si_size didn't get filled in, then we're done */
196 if (psi->si_size == 0 ||
197 !IS_P2ALIGNED(psi->si_size, sizeof (psi))) {
198 break;
201 /* LINTED: (note 1) */
202 psi = (struct sockinfo *)&(((char *)psi)[psi->si_size]);
206 static char *
207 typetoname(t_scalar_t type)
209 switch (type) {
210 case T_CLTS:
211 return ("dgram");
213 case T_COTS:
214 return ("stream");
216 case T_COTS_ORD:
217 return ("stream-ord");
219 default:
220 return ("");
225 * nextstr(): find the beginning of a next string.
226 * The sockfs kstat left-justifies each address string, leaving
227 * null's between the strings. Since we don't necessarily know
228 * the sizes of pointers in the kernel, we need to skip over these
229 * nulls in order to get to the start of the next string.
231 static char *
232 nextstr(char *pas)
234 char *next;
236 for (next = &pas[strlen(pas) + 1]; *next == '\0'; ) {
237 next++;
240 return (next);