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

1.21    ! guenther    1: /*     $OpenBSD: unix.c,v 1.20 2013/08/18 16:32:24 guenther 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: #include <sys/file.h>
1.21    ! guenther   46: #undef _KERNEL
1.1       deraadt    47:
                     48: #include <netinet/in.h>
                     49:
1.5       millert    50: #include <limits.h>
1.1       deraadt    51: #include <stdio.h>
                     52: #include <stdlib.h>
                     53: #include <kvm.h>
                     54: #include "netstat.h"
                     55:
1.21    ! guenther   56: static void unixdomainpr(const struct kinfo_file *, u_long);
1.1       deraadt    57:
                     58: void
1.21    ! guenther   59: unixpr(kvm_t *kvmd, u_long pcbaddr)
1.1       deraadt    60: {
1.21    ! guenther   61:        struct kinfo_file *kf;
1.1       deraadt    62:        struct socket sock, *so = &sock;
1.21    ! guenther   63:        int i, fcnt;
1.1       deraadt    64:
1.21    ! guenther   65:        kf = kvm_getfiles(kvmd, KERN_FILE_BYFILE, 0, sizeof(*kf), &fcnt);
        !            66:        if (kf == NULL) {
1.1       deraadt    67:                printf("Out of memory (file table).\n");
                     68:                return;
                     69:        }
1.21    ! guenther   70:        for (i = 0; i < fcnt; i++) {
        !            71:                if (kf[i].f_count != 0 && kf[i].f_type == DTYPE_SOCKET &&
        !            72:                    kf[i].so_family == AF_LOCAL && (kf[i].so_pcb != 0 ||
        !            73:                    kf[i].unp_path[0] != '\0'))
        !            74:                        unixdomainpr(&kf[i], pcbaddr);
1.1       deraadt    75:        }
                     76: }
                     77:
1.21    ! guenther   78: static const char *socktype[] =
1.1       deraadt    79:     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
                     80:
                     81: static void
1.21    ! guenther   82: unixdomainpr(const struct kinfo_file *kf, u_long pcbaddr)
1.1       deraadt    83: {
                     84:        static int first = 1;
                     85:
1.15      bluhm      86:        if (Pflag) {
1.21    ! guenther   87:                if (pcbaddr == kf->f_data)
1.15      bluhm      88:                        socket_dump(pcbaddr);
                     89:                return;
                     90:        }
                     91:
1.1       deraadt    92:        if (first) {
                     93:                printf("Active UNIX domain sockets\n");
1.11      deraadt    94:                printf("%-*.*s %-6.6s %-6.6s %-6.6s %*.*s %*.*s %*.*s %*.*s Addr\n",
1.5       millert    95:                    PLEN, PLEN, "Address", "Type", "Recv-Q", "Send-Q",
                     96:                    PLEN, PLEN, "Inode", PLEN, PLEN, "Conn",
                     97:                    PLEN, PLEN, "Refs", PLEN, PLEN, "Nextref");
1.1       deraadt    98:                first = 0;
                     99:        }
1.21    ! guenther  100:
        !           101: #define        FAKE_PTR(p)     (PLEN - ((p) ? 0 : 2)), p, ((p) ? "" : "x0")
        !           102:        printf("%#*llx%s %-6.6s %6ld %6ld %#*llx%s %#*llx%s %#*llx%s %#*llx%s",
        !           103:            FAKE_PTR(kf->f_data), socktype[kf->so_type],
        !           104:            kf->so_rcv_cc, kf->so_snd_cc,
        !           105:            FAKE_PTR(kf->v_un),
        !           106:            FAKE_PTR(kf->unp_conn),
        !           107:            FAKE_PTR(kf->unp_refs),
        !           108:            FAKE_PTR(kf->unp_nextref));
        !           109:        if (kf->unp_path[0] != '\0')
        !           110:                printf(" %.*s", KI_UNPPATHLEN, kf->unp_path);
1.1       deraadt   111:        putchar('\n');
1.15      bluhm     112: }
                    113:
                    114: /*
                    115:  * Dump the contents of a UNIX PCB
                    116:  */
                    117: void
                    118: unpcb_dump(u_long off)
                    119: {
                    120:        struct unpcb unp;
                    121:
                    122:        if (off == 0)
                    123:                return;
                    124:        kread(off, &unp, sizeof(unp));
                    125:
1.18      deraadt   126: #define        p(fmt, v, sep) printf(#v " " fmt sep, unp.v);
                    127: #define        pll(fmt, v, sep) printf(#v " " fmt sep, (long long) unp.v);
1.19      deraadt   128: #define        pull(fmt, v, sep) printf(#v " " fmt sep, (unsigned long long) unp.v);
1.16      deraadt   129: #define        pp(fmt, v, sep) printf(#v " " fmt sep, hideroot ? 0 : unp.v);
                    130:        printf("unpcb %#lx\n ", hideroot ? 0 : off);
                    131:        pp("%p", unp_socket, "\n ");
                    132:        pp("%p", unp_vnode, ", ");
1.19      deraadt   133:        pull("%llu", unp_ino, "\n ");
1.16      deraadt   134:        pp("%p", unp_conn, ", ");
                    135:        pp("%p", unp_refs, ", ");
                    136:        pp("%p", unp_nextref, "\n ");
                    137:        pp("%p", unp_addr, "\n ");
1.20      guenther  138:        p("%#.8x", unp_flags, "\n ");
1.18      deraadt   139:        p("%u", unp_connid.uid, ", ");
                    140:        p("%u", unp_connid.gid, ", ");
                    141:        p("%d", unp_connid.pid, "\n ");
                    142:        p("%d", unp_cc, ", ");
                    143:        p("%d", unp_mbcnt, "\n ");
                    144:        pll("%lld", unp_ctime.tv_sec, ", ");
                    145:        p("%ld", unp_ctime.tv_nsec, "\n");
1.15      bluhm     146: #undef p
1.16      deraadt   147: #undef pp
1.1       deraadt   148: }