Home | History | Annotate | Line # | Download | only in netstat
unix.c revision 1.25
      1 /*	$NetBSD: unix.c,v 1.25 2006/05/11 01:23:20 mrg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1983, 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "from: @(#)unix.c	8.1 (Berkeley) 6/6/93";
     36 #else
     37 __RCSID("$NetBSD: unix.c,v 1.25 2006/05/11 01:23:20 mrg Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 /*
     42  * Display protocol blocks in the unix domain.
     43  */
     44 #include <sys/param.h>
     45 #include <sys/protosw.h>
     46 #include <sys/socket.h>
     47 #include <sys/socketvar.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/sysctl.h>
     50 #include <sys/un.h>
     51 #include <sys/unpcb.h>
     52 #define _KERNEL
     53 struct uio;
     54 struct proc;
     55 #include <sys/file.h>
     56 
     57 #include <netinet/in.h>
     58 
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <kvm.h>
     63 #include <err.h>
     64 #include "netstat.h"
     65 
     66 static	void unixdomainprhdr(void);
     67 static	void unixdomainpr0(u_long, u_long, u_long, u_long, u_long, u_long,
     68 			   u_long, u_long, u_long, struct sockaddr_un *, int);
     69 static	void unixdomainpr(struct socket *, caddr_t);
     70 
     71 static struct	file *file, *fileNFILE;
     72 static int	ns_nfiles;
     73 extern	kvm_t *kvmd;
     74 
     75 static void
     76 unixdomainprhdr(void)
     77 {
     78 	printf("Active UNIX domain sockets\n");
     79 	printf("%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
     80 	       "Address", "Type", "Recv-Q", "Send-Q", "Inode", "Conn", "Refs",
     81 	       "Nextref");
     82 }
     83 
     84 static	char *socktype[] =
     85     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
     86 
     87 static void
     88 unixdomainpr0(u_long so_pcb, u_long so_type, u_long rcvq, u_long sndq,
     89 	      u_long inode, u_long conn, u_long refs, u_long nextref,
     90 	      u_long addr, struct sockaddr_un *sun, int remote)
     91 {
     92 	printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
     93 	       so_pcb, socktype[so_type], rcvq, sndq, inode, conn, refs,
     94 	       nextref);
     95 	if (addr || remote)
     96 		printf((remote ? " -> %.*s" : " %.*s"),
     97 		       (int)(sun->sun_len - (sizeof(*sun) - sizeof(sun->sun_path))),
     98 		       sun->sun_path);
     99 	putchar('\n');
    100 }
    101 
    102 static void
    103 unixdomainpr(so, soaddr)
    104 	struct socket *so;
    105 	caddr_t soaddr;
    106 {
    107 	struct unpcb unp, runp;
    108 	struct sockaddr_un sun, rsun;
    109 	static int first = 1;
    110 	int remote = 0;
    111 
    112 	if (kread((u_long)so->so_pcb, (char *)&unp, sizeof (unp)))
    113 		return;
    114 	if (unp.unp_addr)
    115 		if (kread((u_long)unp.unp_addr, (char *)&sun, sizeof (sun)))
    116 			unp.unp_addr = 0;
    117 
    118 	if (!unp.unp_addr) {
    119 		memset(&rsun, 0, sizeof(rsun));
    120 		if (unp.unp_conn &&
    121 		    kread((u_long)unp.unp_conn, (char *)&runp, sizeof (runp)) == 0 &&
    122 		    runp.unp_addr &&
    123 		    kread((u_long)runp.unp_addr, (char *)&rsun, sizeof (rsun)) == 0 &&
    124 		    rsun.sun_path[0] != '\0')
    125 			remote = 1;
    126 	}
    127 
    128 	if (first) {
    129 		unixdomainprhdr();
    130 		first = 0;
    131 	}
    132 
    133 	unixdomainpr0((u_long)so->so_pcb, so->so_type, so->so_rcv.sb_cc,
    134 		      so->so_snd.sb_cc, (u_long)unp.unp_vnode,
    135 		      (u_long)unp.unp_conn, (u_long)unp.unp_refs,
    136 		      (u_long)unp.unp_nextref, (u_long)unp.unp_addr,
    137 		      remote ? &rsun : &sun, remote);
    138 }
    139 
    140 void
    141 unixpr(off)
    142 	u_long	off;
    143 {
    144 	struct file *fp;
    145 	struct socket sock, *so = &sock;
    146 	char *filebuf;
    147 	struct protosw *unixsw = (struct protosw *)off;
    148 
    149 	if (use_sysctl) {
    150 		struct kinfo_pcb *pcblist;
    151 		int mib[8];
    152 		size_t namelen = 0, size = 0, i;
    153 		char *mibname = "net.local.stream.pcblist";
    154 		static int first = 1;
    155 		int done = 0;
    156 
    157  again:
    158 		memset(mib, 0, sizeof(mib));
    159 
    160 		if (sysctlnametomib(mibname, mib,
    161 				    &namelen) == -1)
    162 			err(1, "sysctlnametomib");
    163 
    164 		if (sysctl(mib, sizeof(mib) / sizeof(*mib), NULL, &size,
    165 			   NULL, 0) == -1)
    166 			err(1, "sysctl (query)");
    167 
    168 		pcblist = malloc(size);
    169 		memset(pcblist, 0, size);
    170 
    171 		mib[6] = sizeof(*pcblist);
    172 		mib[7] = size / sizeof(*pcblist);
    173 
    174 		if (sysctl(mib, sizeof(mib) / sizeof(*mib), pcblist,
    175 			   &size, NULL, 0) == -1)
    176 			err(1, "sysctl (copy)");
    177 
    178 		for (i = 0; i < size / sizeof(*pcblist); i++) {
    179 			struct kinfo_pcb *ki = &pcblist[i];
    180 			struct sockaddr_un *sun;
    181 			int remote = 0;
    182 
    183 			if (first) {
    184 				unixdomainprhdr();
    185 				first = 0;
    186 			}
    187 
    188 			sun = (struct sockaddr_un *)&ki->ki_dst;
    189 			if (sun->sun_path[0] != '\0') {
    190 				remote = 1;
    191 			} else {
    192 				sun = (struct sockaddr_un *)&ki->ki_src;
    193 			}
    194 
    195 			unixdomainpr0(ki->ki_pcbaddr, ki->ki_type,
    196 				      ki->ki_rcvq, ki->ki_sndq,
    197 				      ki->ki_vnode, ki->ki_conn, ki->ki_refs,
    198 				      ki->ki_nextref, ki->ki_sockaddr, sun, remote);
    199 		}
    200 
    201 		free(pcblist);
    202 
    203 		if (!done && mibname) {
    204 			mibname = "net.local.dgram.pcblist";
    205 			done = 1;
    206 			goto again;
    207 		}
    208 	} else {
    209 		filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &ns_nfiles);
    210 		if (filebuf == 0) {
    211 			printf("file table read error: %s", kvm_geterr(kvmd));
    212 			return;
    213 		}
    214 		file = (struct file *)(filebuf + sizeof(fp));
    215 		fileNFILE = file + ns_nfiles;
    216 		for (fp = file; fp < fileNFILE; fp++) {
    217 			if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
    218 				continue;
    219 			if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
    220 				continue;
    221 			/* kludge */
    222 			if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
    223 				if (so->so_pcb)
    224 					unixdomainpr(so, fp->f_data);
    225 		}
    226 	}
    227 }
    228