unix.c revision 14543
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>
4314543Sdg#include <sys/queue.h>
441590Srgrimes#include <sys/protosw.h>
451590Srgrimes#include <sys/socket.h>
461590Srgrimes#include <sys/socketvar.h>
471590Srgrimes#include <sys/mbuf.h>
481590Srgrimes#include <sys/sysctl.h>
491590Srgrimes#include <sys/un.h>
501590Srgrimes#include <sys/unpcb.h>
511590Srgrimes#define KERNEL
521590Srgrimesstruct uio;
531590Srgrimesstruct proc;
541590Srgrimes#include <sys/file.h>
551590Srgrimes
561590Srgrimes#include <netinet/in.h>
571590Srgrimes
581590Srgrimes#include <stdio.h>
591590Srgrimes#include <stdlib.h>
601590Srgrimes#include "netstat.h"
611590Srgrimes
621590Srgrimesstatic	void unixdomainpr __P((struct socket *, caddr_t));
631590Srgrimes
641590Srgrimesstatic struct	file *file, *fileNFILE;
651590Srgrimesstatic int	nfiles;
661590Srgrimesextern	kvm_t *kvmd;
671590Srgrimes
681590Srgrimesvoid
691590Srgrimesunixpr(off)
701590Srgrimes	u_long	off;
711590Srgrimes{
721590Srgrimes	register struct file *fp;
731590Srgrimes	struct socket sock, *so = &sock;
741590Srgrimes	char *filebuf;
751590Srgrimes	struct protosw *unixsw = (struct protosw *)off;
761590Srgrimes
771590Srgrimes	filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
781590Srgrimes	if (filebuf == 0) {
791590Srgrimes		printf("Out of memory (file table).\n");
801590Srgrimes		return;
811590Srgrimes	}
821590Srgrimes	file = (struct file *)(filebuf + sizeof(fp));
831590Srgrimes	fileNFILE = file + nfiles;
841590Srgrimes	for (fp = file; fp < fileNFILE; fp++) {
851590Srgrimes		if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
861590Srgrimes			continue;
871590Srgrimes		if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
881590Srgrimes			continue;
891590Srgrimes		/* kludge */
901590Srgrimes		if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
911590Srgrimes			if (so->so_pcb)
921590Srgrimes				unixdomainpr(so, fp->f_data);
931590Srgrimes	}
941590Srgrimes}
951590Srgrimes
961590Srgrimesstatic	char *socktype[] =
971590Srgrimes    { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
981590Srgrimes
991590Srgrimesstatic void
1001590Srgrimesunixdomainpr(so, soaddr)
1011590Srgrimes	register struct socket *so;
1021590Srgrimes	caddr_t soaddr;
1031590Srgrimes{
1041590Srgrimes	struct unpcb unpcb, *unp = &unpcb;
1051590Srgrimes	struct mbuf mbuf, *m;
1061590Srgrimes	struct sockaddr_un *sa;
1071590Srgrimes	static int first = 1;
1081590Srgrimes
1091590Srgrimes	if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
1101590Srgrimes		return;
1111590Srgrimes	if (unp->unp_addr) {
1121590Srgrimes		m = &mbuf;
1131590Srgrimes		if (kread((u_long)unp->unp_addr, (char *)m, sizeof (*m)))
1141590Srgrimes			m = (struct mbuf *)0;
1151590Srgrimes		sa = (struct sockaddr_un *)(m->m_dat);
1161590Srgrimes	} else
1171590Srgrimes		m = (struct mbuf *)0;
1181590Srgrimes	if (first) {
1191590Srgrimes		printf("Active UNIX domain sockets\n");
1201590Srgrimes		printf(
1211590Srgrimes"%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
1221590Srgrimes		    "Address", "Type", "Recv-Q", "Send-Q",
1231590Srgrimes		    "Inode", "Conn", "Refs", "Nextref");
1241590Srgrimes		first = 0;
1251590Srgrimes	}
1261590Srgrimes	printf("%8x %-6.6s %6d %6d %8x %8x %8x %8x",
1271590Srgrimes	    soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
1281590Srgrimes	    unp->unp_vnode, unp->unp_conn,
1291590Srgrimes	    unp->unp_refs, unp->unp_nextref);
1301590Srgrimes	if (m)
1311590Srgrimes		printf(" %.*s",
1321590Srgrimes		    m->m_len - (int)(sizeof(*sa) - sizeof(sa->sun_path)),
1331590Srgrimes		    sa->sun_path);
1341590Srgrimes	putchar('\n');
1351590Srgrimes}
136