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]
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
34 * University Acknowledgment- Portions of this document are derived from
35 * software developed by the University of California, Berkeley, and its
40 * code for netstat's -k option
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.
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.
75 unixpr(kstat_ctl_t
*kc
)
79 if (kc
== NULL
) { /* sanity check. */
80 fail(0, "unixpr: No kstat");
84 /* find the sockfs kstat: */
85 if ((ksp
= kstat_lookup(kc
, "sockfs", 0, "sock_unix_list")) ==
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");
98 print_kn(kstat_t
*ksp
)
100 char nullstr
[NALEN
+ 1];
102 struct sockinfo
*psi
; /* ptr to current sockinfo */
103 char *pas
; /* ptr to string-format addrs */
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: */
142 local_vp
= conn_vp
= nullstr
;
144 if ((psi
->si_state
& SS_ISBOUND
) &&
145 (psi
->si_ux_laddr_sou_magic
== SOU_MAGIC_EXPLICIT
)) {
149 /* faddr.sou_vp: 3rd string after sockinfo: */
151 if ((psi
->si_state
& SS_ISCONNECTED
) &&
152 (psi
->si_ux_faddr_sou_magic
== SOU_MAGIC_EXPLICIT
)) {
156 (void) printf("%s %s ", local_vp
, conn_vp
);
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) ");
165 if (psi
->si_laddr_soa_len
>
166 sizeof (psi
->si_laddr_family
))
168 psi
->si_laddr_sun_path
);
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) ");
183 if (psi
->si_faddr_soa_len
>
184 sizeof (psi
->si_faddr_family
))
186 psi
->si_faddr_sun_path
);
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
))) {
201 /* LINTED: (note 1) */
202 psi
= (struct sockinfo
*)&(((char *)psi
)[psi
->si_size
]);
207 typetoname(t_scalar_t type
)
217 return ("stream-ord");
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.
236 for (next
= &pas
[strlen(pas
) + 1]; *next
== '\0'; ) {