[BACK]Return to unix.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / netstat

Annotation of src/usr.bin/netstat/unix.c, Revision 1.1.1.1

1.1       deraadt     1: /*     $NetBSD: unix.c,v 1.13 1995/10/03 21:42:48 thorpej 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. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: #if 0
                     38: static char sccsid[] = "from: @(#)unix.c       8.1 (Berkeley) 6/6/93";
                     39: #else
                     40: static char *rcsid = "$NetBSD: unix.c,v 1.13 1995/10/03 21:42:48 thorpej Exp $";
                     41: #endif
                     42: #endif /* not lint */
                     43:
                     44: /*
                     45:  * Display protocol blocks in the unix domain.
                     46:  */
                     47: #include <sys/param.h>
                     48: #include <sys/protosw.h>
                     49: #include <sys/socket.h>
                     50: #include <sys/socketvar.h>
                     51: #include <sys/mbuf.h>
                     52: #include <sys/sysctl.h>
                     53: #include <sys/un.h>
                     54: #include <sys/unpcb.h>
                     55: #define _KERNEL
                     56: struct uio;
                     57: struct proc;
                     58: #include <sys/file.h>
                     59:
                     60: #include <netinet/in.h>
                     61:
                     62: #include <stdio.h>
                     63: #include <stdlib.h>
                     64: #include <kvm.h>
                     65: #include "netstat.h"
                     66:
                     67: static void unixdomainpr __P((struct socket *, caddr_t));
                     68:
                     69: static struct  file *file, *fileNFILE;
                     70: static int     nfiles;
                     71: extern kvm_t *kvmd;
                     72:
                     73: void
                     74: unixpr(off)
                     75:        u_long  off;
                     76: {
                     77:        register struct file *fp;
                     78:        struct socket sock, *so = &sock;
                     79:        char *filebuf;
                     80:        struct protosw *unixsw = (struct protosw *)off;
                     81:
                     82:        filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
                     83:        if (filebuf == 0) {
                     84:                printf("Out of memory (file table).\n");
                     85:                return;
                     86:        }
                     87:        file = (struct file *)(filebuf + sizeof(fp));
                     88:        fileNFILE = file + nfiles;
                     89:        for (fp = file; fp < fileNFILE; fp++) {
                     90:                if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
                     91:                        continue;
                     92:                if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
                     93:                        continue;
                     94:                /* kludge */
                     95:                if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
                     96:                        if (so->so_pcb)
                     97:                                unixdomainpr(so, fp->f_data);
                     98:        }
                     99: }
                    100:
                    101: static char *socktype[] =
                    102:     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
                    103:
                    104: static void
                    105: unixdomainpr(so, soaddr)
                    106:        register struct socket *so;
                    107:        caddr_t soaddr;
                    108: {
                    109:        struct unpcb unpcb, *unp = &unpcb;
                    110:        struct mbuf mbuf, *m;
                    111:        struct sockaddr_un *sa;
                    112:        static int first = 1;
                    113:
                    114:        if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
                    115:                return;
                    116:        if (unp->unp_addr) {
                    117:                m = &mbuf;
                    118:                if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
                    119:                        m = (struct mbuf *)0;
                    120:                sa = (struct sockaddr_un *)(m->m_dat);
                    121:        } else
                    122:                m = (struct mbuf *)0;
                    123:        if (first) {
                    124:                printf("Active UNIX domain sockets\n");
                    125:                printf(
                    126: "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
                    127:                    "Address", "Type", "Recv-Q", "Send-Q",
                    128:                    "Inode", "Conn", "Refs", "Nextref");
                    129:                first = 0;
                    130:        }
                    131:        printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
                    132:            soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
                    133:            unp->unp_vnode, unp->unp_conn,
                    134:            unp->unp_refs, unp->unp_nextref);
                    135:        if (m)
                    136:                printf(" %.*s",
                    137:                    m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path)),
                    138:                    sa->sun_path);
                    139:        putchar('\n');
                    140: }