Home | History | Annotate | Line # | Download | only in netstat
unix.c revision 1.21
      1  1.21       agc /*	$NetBSD: unix.c,v 1.21 2003/08/07 11:15:22 agc Exp $	*/
      2  1.13   thorpej 
      3   1.1       cgd /*-
      4  1.10   mycroft  * Copyright (c) 1983, 1988, 1993
      5  1.10   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.21       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.17     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.13   thorpej #if 0
     35  1.13   thorpej static char sccsid[] = "from: @(#)unix.c	8.1 (Berkeley) 6/6/93";
     36  1.13   thorpej #else
     37  1.21       agc __RCSID("$NetBSD: unix.c,v 1.21 2003/08/07 11:15:22 agc Exp $");
     38  1.13   thorpej #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd /*
     42   1.1       cgd  * Display protocol blocks in the unix domain.
     43   1.1       cgd  */
     44   1.1       cgd #include <sys/param.h>
     45   1.1       cgd #include <sys/protosw.h>
     46   1.1       cgd #include <sys/socket.h>
     47   1.1       cgd #include <sys/socketvar.h>
     48   1.1       cgd #include <sys/mbuf.h>
     49  1.10   mycroft #include <sys/sysctl.h>
     50   1.1       cgd #include <sys/un.h>
     51   1.1       cgd #include <sys/unpcb.h>
     52  1.12       jtc #define _KERNEL
     53   1.1       cgd struct uio;
     54  1.10   mycroft struct proc;
     55   1.1       cgd #include <sys/file.h>
     56   1.1       cgd 
     57  1.10   mycroft #include <netinet/in.h>
     58  1.10   mycroft 
     59  1.10   mycroft #include <stdio.h>
     60  1.10   mycroft #include <stdlib.h>
     61  1.11   deraadt #include <kvm.h>
     62  1.10   mycroft #include "netstat.h"
     63  1.10   mycroft 
     64  1.10   mycroft static	void unixdomainpr __P((struct socket *, caddr_t));
     65  1.10   mycroft 
     66  1.10   mycroft static struct	file *file, *fileNFILE;
     67  1.10   mycroft static int	nfiles;
     68  1.10   mycroft extern	kvm_t *kvmd;
     69   1.1       cgd 
     70  1.10   mycroft void
     71  1.10   mycroft unixpr(off)
     72  1.10   mycroft 	u_long	off;
     73   1.1       cgd {
     74  1.17     lukem 	struct file *fp;
     75   1.1       cgd 	struct socket sock, *so = &sock;
     76  1.10   mycroft 	char *filebuf;
     77  1.10   mycroft 	struct protosw *unixsw = (struct protosw *)off;
     78   1.1       cgd 
     79  1.10   mycroft 	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
     80  1.10   mycroft 	if (filebuf == 0) {
     81  1.18       cgd 		printf("file table read error: %s", kvm_geterr(kvmd));
     82   1.1       cgd 		return;
     83   1.1       cgd 	}
     84  1.10   mycroft 	file = (struct file *)(filebuf + sizeof(fp));
     85   1.1       cgd 	fileNFILE = file + nfiles;
     86   1.1       cgd 	for (fp = file; fp < fileNFILE; fp++) {
     87   1.1       cgd 		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
     88   1.1       cgd 			continue;
     89  1.10   mycroft 		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
     90   1.1       cgd 			continue;
     91   1.1       cgd 		/* kludge */
     92   1.1       cgd 		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
     93   1.1       cgd 			if (so->so_pcb)
     94   1.1       cgd 				unixdomainpr(so, fp->f_data);
     95   1.1       cgd 	}
     96   1.1       cgd }
     97   1.1       cgd 
     98   1.1       cgd static	char *socktype[] =
     99   1.1       cgd     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
    100   1.1       cgd 
    101  1.10   mycroft static void
    102   1.1       cgd unixdomainpr(so, soaddr)
    103  1.17     lukem 	struct socket *so;
    104   1.1       cgd 	caddr_t soaddr;
    105   1.1       cgd {
    106  1.14   mycroft 	struct unpcb unp;
    107  1.14   mycroft 	struct sockaddr_un sun;
    108   1.1       cgd 	static int first = 1;
    109   1.1       cgd 
    110  1.14   mycroft 	if (kread((u_long)so->so_pcb, (char *)&unp, sizeof (unp)))
    111   1.1       cgd 		return;
    112  1.14   mycroft 	if (unp.unp_addr)
    113  1.14   mycroft 		if (kread((u_long)unp.unp_addr, (char *)&sun, sizeof (sun)))
    114  1.14   mycroft 			unp.unp_addr = 0;
    115   1.1       cgd 	if (first) {
    116   1.1       cgd 		printf("Active UNIX domain sockets\n");
    117   1.1       cgd 		printf(
    118   1.1       cgd "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
    119   1.1       cgd 		    "Address", "Type", "Recv-Q", "Send-Q",
    120   1.1       cgd 		    "Inode", "Conn", "Refs", "Nextref");
    121   1.1       cgd 		first = 0;
    122   1.1       cgd 	}
    123  1.16  christos 	printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
    124  1.16  christos 	    (u_long) soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
    125  1.16  christos 	    (u_long) unp.unp_vnode, (u_long) unp.unp_conn,
    126  1.16  christos 	    (u_long) unp.unp_refs, (u_long) unp.unp_nextref);
    127  1.14   mycroft 	if (unp.unp_addr)
    128  1.15     mikel 		printf(" %.*s",
    129  1.16  christos 		    (int) (sun.sun_len - (sizeof(sun) - sizeof(sun.sun_path))),
    130  1.15     mikel 		    sun.sun_path);
    131   1.1       cgd 	putchar('\n');
    132   1.1       cgd }
    133