unix.c revision 1590
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1983, 1988, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
351590Srgrimesstatic char sccsid[] = "@(#)unix.c	8.1 (Berkeley) 6/6/93";
361590Srgrimes#endif /* not lint */
371590Srgrimes
381590Srgrimes/*
391590Srgrimes * Display protocol blocks in the unix domain.
401590Srgrimes */
411590Srgrimes#include <kvm.h>
421590Srgrimes#include <sys/param.h>
431590Srgrimes#include <sys/protosw.h>
441590Srgrimes#include <sys/socket.h>
451590Srgrimes#include <sys/socketvar.h>
461590Srgrimes#include <sys/mbuf.h>
471590Srgrimes#include <sys/sysctl.h>
481590Srgrimes#include <sys/un.h>
491590Srgrimes#include <sys/unpcb.h>
501590Srgrimes#define KERNEL
511590Srgrimesstruct uio;
521590Srgrimesstruct proc;
531590Srgrimes#include <sys/file.h>
541590Srgrimes
551590Srgrimes#include <netinet/in.h>
561590Srgrimes
571590Srgrimes#include <stdio.h>
581590Srgrimes#include <stdlib.h>
591590Srgrimes#include "netstat.h"
601590Srgrimes
611590Srgrimesstatic	void unixdomainpr __P((struct socket *, caddr_t));
621590Srgrimes
631590Srgrimesstatic struct	file *file, *fileNFILE;
641590Srgrimesstatic int	nfiles;
651590Srgrimesextern	kvm_t *kvmd;
661590Srgrimes
671590Srgrimesvoid
681590Srgrimesunixpr(off)
691590Srgrimes	u_long	off;
701590Srgrimes{
711590Srgrimes	register struct file *fp;
721590Srgrimes	struct socket sock, *so = &sock;
731590Srgrimes	char *filebuf;
741590Srgrimes	struct protosw *unixsw = (struct protosw *)off;
751590Srgrimes
761590Srgrimes	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
771590Srgrimes	if (filebuf == 0) {
781590Srgrimes		printf("Out of memory (file table).\n");
791590Srgrimes		return;
801590Srgrimes	}
811590Srgrimes	file = (struct file *)(filebuf + sizeof(fp));
821590Srgrimes	fileNFILE = file + nfiles;
831590Srgrimes	for (fp = file; fp < fileNFILE; fp++) {
841590Srgrimes		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
851590Srgrimes			continue;
861590Srgrimes		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
871590Srgrimes			continue;
881590Srgrimes		/* kludge */
891590Srgrimes		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
901590Srgrimes			if (so->so_pcb)
911590Srgrimes				unixdomainpr(so, fp->f_data);
921590Srgrimes	}
931590Srgrimes}
941590Srgrimes
951590Srgrimesstatic	char *socktype[] =
961590Srgrimes    { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
971590Srgrimes
981590Srgrimesstatic void
991590Srgrimesunixdomainpr(so, soaddr)
1001590Srgrimes	register struct socket *so;
1011590Srgrimes	caddr_t soaddr;
1021590Srgrimes{
1031590Srgrimes	struct unpcb unpcb, *unp = &unpcb;
1041590Srgrimes	struct mbuf mbuf, *m;
1051590Srgrimes	struct sockaddr_un *sa;
1061590Srgrimes	static int first = 1;
1071590Srgrimes
1081590Srgrimes	if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
1091590Srgrimes		return;
1101590Srgrimes	if (unp->unp_addr) {
1111590Srgrimes		m = &mbuf;
1121590Srgrimes		if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
1131590Srgrimes			m = (struct mbuf *)0;
1141590Srgrimes		sa = (struct sockaddr_un *)(m->m_dat);
1151590Srgrimes	} else
1161590Srgrimes		m = (struct mbuf *)0;
1171590Srgrimes	if (first) {
1181590Srgrimes		printf("Active UNIX domain sockets\n");
1191590Srgrimes		printf(
1201590Srgrimes"%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
1211590Srgrimes		    "Address", "Type", "Recv-Q", "Send-Q",
1221590Srgrimes		    "Inode", "Conn", "Refs", "Nextref");
1231590Srgrimes		first = 0;
1241590Srgrimes	}
1251590Srgrimes	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
1261590Srgrimes	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
1271590Srgrimes	    unp->unp_vnode, unp->unp_conn,
1281590Srgrimes	    unp->unp_refs, unp->unp_nextref);
1291590Srgrimes	if (m)
1301590Srgrimes		printf(" %.*s",
1311590Srgrimes		    m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path)),
1321590Srgrimes		    sa->sun_path);
1331590Srgrimes	putchar('\n');
1341590Srgrimes}
135