Annotation of 43BSDReno/usr.bin/netstat/unix.c, revision 1.1

1.1     ! root        1: /*-
        !             2:  * Copyright (c) 1983, 1988 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted provided
        !             6:  * that: (1) source distributions retain this entire copyright notice and
        !             7:  * comment, and (2) distributions including binaries display the following
        !             8:  * acknowledgement:  ``This product includes software developed by the
        !             9:  * University of California, Berkeley and its contributors'' in the
        !            10:  * documentation or other materials provided with the distribution and in
        !            11:  * all advertising materials mentioning features or use of this software.
        !            12:  * Neither the name of the University nor the names of its contributors may
        !            13:  * be used to endorse or promote products derived from this software without
        !            14:  * specific prior written permission.
        !            15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
        !            16:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
        !            17:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            18:  */
        !            19: 
        !            20: #ifndef lint
        !            21: static char sccsid[] = "@(#)unix.c     5.9 (Berkeley) 6/18/90";
        !            22: #endif /* not lint */
        !            23: 
        !            24: /*
        !            25:  * Display protocol blocks in the unix domain.
        !            26:  */
        !            27: #include <sys/param.h>
        !            28: #include <sys/protosw.h>
        !            29: #include <sys/socket.h>
        !            30: #include <sys/socketvar.h>
        !            31: #include <sys/mbuf.h>
        !            32: #include <sys/un.h>
        !            33: #include <sys/unpcb.h>
        !            34: #define        KERNEL
        !            35: #include <sys/file.h>
        !            36: 
        !            37: int    Aflag;
        !            38: extern char *calloc();
        !            39: 
        !            40: unixpr(nfileaddr, fileaddr, unixsw)
        !            41:        off_t nfileaddr, fileaddr;
        !            42:        struct protosw *unixsw;
        !            43: {
        !            44:        register struct file *fp;
        !            45:        struct file *filep;
        !            46:        struct socket sock, *so = &sock;
        !            47: 
        !            48:        if (nfileaddr == 0 || fileaddr == 0) {
        !            49:                printf("nfile or file not in namelist.\n");
        !            50:                return;
        !            51:        }
        !            52:        if (kvm_read(nfileaddr, (char *)&nfile, sizeof (nfile)) !=
        !            53:                                                sizeof (nfile)) {
        !            54:                printf("nfile: bad read.\n");
        !            55:                return;
        !            56:        }
        !            57:        if (kvm_read(fileaddr, (char *)&filep, sizeof (filep))
        !            58:                                                != sizeof (filep)) {
        !            59:                printf("File table address, bad read.\n");
        !            60:                return;
        !            61:        }
        !            62:        file = (struct file *)calloc(nfile, sizeof (struct file));
        !            63:        if (file == (struct file *)0) {
        !            64:                printf("Out of memory (file table).\n");
        !            65:                return;
        !            66:        }
        !            67:        if (kvm_read((off_t)filep, (char *)file, nfile * sizeof (struct file))
        !            68:                                        != nfile * sizeof (struct file)) {
        !            69:                printf("File table read error.\n");
        !            70:                return;
        !            71:        }
        !            72:        fileNFILE = file + nfile;
        !            73:        for (fp = file; fp < fileNFILE; fp++) {
        !            74:                if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
        !            75:                        continue;
        !            76:                if (kvm_read((off_t)fp->f_data, (char *)so, sizeof (*so))
        !            77:                                        != sizeof (*so))
        !            78:                        continue;
        !            79:                /* kludge */
        !            80:                if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
        !            81:                        if (so->so_pcb)
        !            82:                                unixdomainpr(so, fp->f_data);
        !            83:        }
        !            84:        free((char *)file);
        !            85: }
        !            86: 
        !            87: static char *socktype[] =
        !            88:     { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
        !            89: 
        !            90: unixdomainpr(so, soaddr)
        !            91:        register struct socket *so;
        !            92:        caddr_t soaddr;
        !            93: {
        !            94:        struct unpcb unpcb, *unp = &unpcb;
        !            95:        struct mbuf mbuf, *m;
        !            96:        struct sockaddr_un *sa;
        !            97:        static int first = 1;
        !            98: 
        !            99:        if (kvm_read((off_t)so->so_pcb, (char *)unp, sizeof (*unp))
        !           100:                                != sizeof (*unp))
        !           101:                return;
        !           102:        if (unp->unp_addr) {
        !           103:                m = &mbuf;
        !           104:                if (kvm_read((off_t)unp->unp_addr, (char *)m, sizeof (*m))
        !           105:                                != sizeof (*m))
        !           106:                        m = (struct mbuf *)0;
        !           107:                sa = (struct sockaddr_un *)(m->m_dat);
        !           108:        } else
        !           109:                m = (struct mbuf *)0;
        !           110:        if (first) {
        !           111:                printf("Active UNIX domain sockets\n");
        !           112:                printf(
        !           113: "%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
        !           114:                    "Address", "Type", "Recv-Q", "Send-Q",
        !           115:                    "Inode", "Conn", "Refs", "Nextref");
        !           116:                first = 0;
        !           117:        }
        !           118:        printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
        !           119:            soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
        !           120:            unp->unp_vnode, unp->unp_conn,
        !           121:            unp->unp_refs, unp->unp_nextref);
        !           122:        if (m)
        !           123:                printf(" %.*s", m->m_len - sizeof(sa->sun_family),
        !           124:                    sa->sun_path);
        !           125:        putchar('\n');
        !           126: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.