[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.15

1.15    ! bluhm       1: /*     $OpenBSD: unix.c,v 1.14 2010/04/28 18:22:11 jsg Exp $   */
1.1       deraadt     2: /*     $NetBSD: unix.c,v 1.13 1995/10/03 21:42:48 thorpej Exp $        */
                      3:
                      4: /*-
                      5:  * Copyright (c) 1983, 1988, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.10      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: /*
                     34:  * Display protocol blocks in the unix domain.
                     35:  */
                     36: #include <sys/param.h>
                     37: #include <sys/protosw.h>
                     38: #include <sys/socket.h>
                     39: #include <sys/socketvar.h>
                     40: #include <sys/mbuf.h>
                     41: #include <sys/sysctl.h>
                     42: #include <sys/un.h>
                     43: #include <sys/unpcb.h>
                     44: #define _KERNEL
                     45: struct uio;
                     46: struct proc;
                     47: #include <sys/file.h>
                     48:
                     49: #include <netinet/in.h>
                     50:
1.5       millert    51: #include <limits.h>
1.1       deraadt    52: #include <stdio.h>
                     53: #include <stdlib.h>
                     54: #include <kvm.h>
                     55: #include "netstat.h"
                     56:
1.15    ! bluhm      57: static void unixdomainpr(struct socket *, caddr_t, u_long);
1.1       deraadt    58:
                     59: static struct  file *file, *fileNFILE;
1.14      jsg        60: static int     fcnt;
1.1       deraadt    61: extern kvm_t *kvmd;
                     62:
                     63: void
1.15    ! bluhm      64: unixpr(u_long off, u_long pcbaddr)
1.1       deraadt    65: {
1.7       mpech      66:        struct file *fp;
1.1       deraadt    67:        struct socket sock, *so = &sock;
                     68:        char *filebuf;
                     69:        struct protosw *unixsw = (struct protosw *)off;
                     70:
1.14      jsg        71:        filebuf = kvm_getfiles(kvmd, KERN_FILE, 0, &fcnt);
1.11      deraadt    72:        if (filebuf == NULL) {
1.1       deraadt    73:                printf("Out of memory (file table).\n");
                     74:                return;
                     75:        }
                     76:        file = (struct file *)(filebuf + sizeof(fp));
1.14      jsg        77:        fileNFILE = file + fcnt;
1.1       deraadt    78:        for (fp = file; fp < fileNFILE; fp++) {
                     79:                if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
                     80:                        continue;
1.12      jaredy     81:                if (kread((u_long)fp->f_data, so, sizeof (*so)))
1.1       deraadt    82:                        continue;
                     83:                /* kludge */
                     84:                if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
                     85:                        if (so->so_pcb)
1.15    ! bluhm      86:                                unixdomainpr(so, fp->f_data, pcbaddr);
1.1       deraadt    87:        }
                     88: }
                     89:
                     90: static char *socktype[] =
                     91:     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
                     92:
                     93: static void
1.15    ! bluhm      94: unixdomainpr(struct socket *so, caddr_t soaddr, u_long pcbaddr)
1.1       deraadt    95: {
                     96:        struct unpcb unpcb, *unp = &unpcb;
                     97:        struct mbuf mbuf, *m;
1.6       itojun     98:        struct sockaddr_un *sa = NULL;
1.1       deraadt    99:        static int first = 1;
                    100:
1.15    ! bluhm     101:        if (Pflag) {
        !           102:                if (pcbaddr == (u_long)soaddr)
        !           103:                        socket_dump(pcbaddr);
        !           104:                return;
        !           105:        }
        !           106:
1.12      jaredy    107:        if (kread((u_long)so->so_pcb, unp, sizeof (*unp)))
1.1       deraadt   108:                return;
                    109:        if (unp->unp_addr) {
                    110:                m = &mbuf;
1.12      jaredy    111:                if (kread((u_long)unp->unp_addr, m, sizeof (*m)))
1.11      deraadt   112:                        m = NULL;
1.1       deraadt   113:                sa = (struct sockaddr_un *)(m->m_dat);
                    114:        } else
1.11      deraadt   115:                m = NULL;
1.1       deraadt   116:        if (first) {
                    117:                printf("Active UNIX domain sockets\n");
1.11      deraadt   118:                printf("%-*.*s %-6.6s %-6.6s %-6.6s %*.*s %*.*s %*.*s %*.*s Addr\n",
1.5       millert   119:                    PLEN, PLEN, "Address", "Type", "Recv-Q", "Send-Q",
                    120:                    PLEN, PLEN, "Inode", PLEN, PLEN, "Conn",
                    121:                    PLEN, PLEN, "Refs", PLEN, PLEN, "Nextref");
1.1       deraadt   122:                first = 0;
                    123:        }
1.5       millert   124:        printf("%*p %-6.6s %6ld %6ld %*p %*p %*p %*p",
                    125:            PLEN, soaddr, socktype[so->so_type], so->so_rcv.sb_cc,
                    126:            so->so_snd.sb_cc, PLEN, unp->unp_vnode, PLEN, unp->unp_conn,
                    127:            PLEN, unp->unp_refs, PLEN, unp->unp_nextref);
1.1       deraadt   128:        if (m)
                    129:                printf(" %.*s",
1.6       itojun    130:                    (int)(m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path))),
1.1       deraadt   131:                    sa->sun_path);
                    132:        putchar('\n');
1.15    ! bluhm     133: }
        !           134:
        !           135: /*
        !           136:  * Dump the contents of a UNIX PCB
        !           137:  */
        !           138: void
        !           139: unpcb_dump(u_long off)
        !           140: {
        !           141:        struct unpcb unp;
        !           142:
        !           143:        if (off == 0)
        !           144:                return;
        !           145:        kread(off, &unp, sizeof(unp));
        !           146:
        !           147: #define        p(fmt, v, sep) printf(#v " " fmt sep, unp.v);
        !           148:        printf("unpcb %#lx\n ", off);
        !           149:        p("%p", unp_socket, "\n ");
        !           150:        p("%p", unp_vnode, ", ");
        !           151:        p("%u", unp_ino, "\n ");
        !           152:        p("%p", unp_conn, ", ");
        !           153:        p("%p", unp_refs, ", ");
        !           154:        p("%p", unp_nextref, "\n ");
        !           155:        p("%p", unp_addr, "\n ");
        !           156:        p("%#0.8x", unp_flags, "\n ");
        !           157:        p("%u", unp_connid.uid, ", ");
        !           158:        p("%u", unp_connid.gid, ", ");
        !           159:        p("%d", unp_connid.pid, "\n ");
        !           160:        p("%d", unp_cc, ", ");
        !           161:        p("%d", unp_mbcnt, "\n ");
        !           162:        p("%d", unp_ctime.tv_sec, ", ");
        !           163:        p("%ld", unp_ctime.tv_nsec, "\n");
        !           164: #undef p
1.1       deraadt   165: }