unix.c revision 28726
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
3527753Scharnier#if 0
361590Srgrimesstatic char sccsid[] = "@(#)unix.c	8.1 (Berkeley) 6/6/93";
3727753Scharnier#endif
3827753Scharnierstatic const char rcsid[] =
3928726Swollman	"$Id: unix.c,v 1.4 1997/07/29 06:51:41 charnier Exp $";
401590Srgrimes#endif /* not lint */
411590Srgrimes
421590Srgrimes/*
431590Srgrimes * Display protocol blocks in the unix domain.
441590Srgrimes */
451590Srgrimes#include <kvm.h>
461590Srgrimes#include <sys/param.h>
4714543Sdg#include <sys/queue.h>
481590Srgrimes#include <sys/protosw.h>
491590Srgrimes#include <sys/socket.h>
501590Srgrimes#include <sys/socketvar.h>
511590Srgrimes#include <sys/mbuf.h>
521590Srgrimes#include <sys/sysctl.h>
531590Srgrimes#include <sys/un.h>
541590Srgrimes#include <sys/unpcb.h>
551590Srgrimes#define KERNEL
561590Srgrimesstruct uio;
571590Srgrimesstruct proc;
581590Srgrimes#include <sys/file.h>
591590Srgrimes
601590Srgrimes#include <netinet/in.h>
611590Srgrimes
621590Srgrimes#include <stdio.h>
631590Srgrimes#include <stdlib.h>
641590Srgrimes#include "netstat.h"
651590Srgrimes
661590Srgrimesstatic	void unixdomainpr __P((struct socket *, caddr_t));
671590Srgrimes
681590Srgrimesstatic struct	file *file, *fileNFILE;
691590Srgrimesstatic int	nfiles;
701590Srgrimesextern	kvm_t *kvmd;
711590Srgrimes
721590Srgrimesvoid
731590Srgrimesunixpr(off)
741590Srgrimes	u_long	off;
751590Srgrimes{
761590Srgrimes	register struct file *fp;
771590Srgrimes	struct socket sock, *so = &sock;
781590Srgrimes	char *filebuf;
791590Srgrimes	struct protosw *unixsw = (struct protosw *)off;
801590Srgrimes
811590Srgrimes	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
821590Srgrimes	if (filebuf == 0) {
831590Srgrimes		printf("Out of memory (file table).\n");
841590Srgrimes		return;
851590Srgrimes	}
861590Srgrimes	file = (struct file *)(filebuf + sizeof(fp));
871590Srgrimes	fileNFILE = file + nfiles;
881590Srgrimes	for (fp = file; fp < fileNFILE; fp++) {
891590Srgrimes		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
901590Srgrimes			continue;
911590Srgrimes		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
921590Srgrimes			continue;
931590Srgrimes		/* kludge */
941590Srgrimes		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
951590Srgrimes			if (so->so_pcb)
961590Srgrimes				unixdomainpr(so, fp->f_data);
971590Srgrimes	}
981590Srgrimes}
991590Srgrimes
1001590Srgrimesstatic	char *socktype[] =
1011590Srgrimes    { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
1021590Srgrimes
1031590Srgrimesstatic void
1041590Srgrimesunixdomainpr(so, soaddr)
1051590Srgrimes	register struct socket *so;
1061590Srgrimes	caddr_t soaddr;
1071590Srgrimes{
1081590Srgrimes	struct unpcb unpcb, *unp = &unpcb;
10928726Swollman	struct sockaddr_un s_un, *sa = NULL;
1101590Srgrimes	static int first = 1;
1111590Srgrimes
1121590Srgrimes	if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
1131590Srgrimes		return;
1141590Srgrimes	if (unp->unp_addr) {
11528726Swollman		sa = &s_un;
11628726Swollman		if (kread((u_long)unp->unp_addr, (char *)sa, sizeof *sa))
11728726Swollman			sa = (struct sockaddr_un *)0;
1181590Srgrimes	} else
11928726Swollman		sa = (struct sockaddr_un *)0;
1201590Srgrimes	if (first) {
1211590Srgrimes		printf("Active UNIX domain sockets\n");
1221590Srgrimes		printf(
1231590Srgrimes"%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
1241590Srgrimes		    "Address", "Type", "Recv-Q", "Send-Q",
1251590Srgrimes		    "Inode", "Conn", "Refs", "Nextref");
1261590Srgrimes		first = 0;
1271590Srgrimes	}
12816080Salex	printf("%8x %-6.6s %6ld %6ld %8x %8x %8x %8x",
12916080Salex	    (int)soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
13016080Salex	    (int)unp->unp_vnode, (int)unp->unp_conn,
13116080Salex	    (int)unp->unp_refs, (int)unp->unp_nextref);
13228726Swollman	if (sa)
13328726Swollman		printf(" %.*s", sa->sun_len, sa->sun_path);
1341590Srgrimes	putchar('\n');
1351590Srgrimes}
136