unix.c (e4bb0b9a) unix.c (4f81ef50)
1/*-
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 22 unchanged lines hidden (view full) ---

31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)unix.c 8.1 (Berkeley) 6/6/93";
37#endif
38static const char rcsid[] =
1/*-
2 * Copyright (c) 1983, 1988, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 22 unchanged lines hidden (view full) ---

31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)unix.c 8.1 (Berkeley) 6/6/93";
37#endif
38static const char rcsid[] =
39 "$Id: unix.c,v 1.4 1997/07/29 06:51:41 charnier Exp $";
39 "$Id: unix.c,v 1.5 1997/08/25 16:55:00 wollman Exp $";
40#endif /* not lint */
41
42/*
43 * Display protocol blocks in the unix domain.
44 */
40#endif /* not lint */
41
42/*
43 * Display protocol blocks in the unix domain.
44 */
45#include <kvm.h>
46#include <sys/param.h>
47#include <sys/queue.h>
48#include <sys/protosw.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/mbuf.h>
52#include <sys/sysctl.h>
53#include <sys/un.h>
54#include <sys/unpcb.h>
45#include <sys/param.h>
46#include <sys/queue.h>
47#include <sys/protosw.h>
48#include <sys/socket.h>
49#include <sys/socketvar.h>
50#include <sys/mbuf.h>
51#include <sys/sysctl.h>
52#include <sys/un.h>
53#include <sys/unpcb.h>
55#define KERNEL
56struct uio;
57struct proc;
58#include <sys/file.h>
59
60#include <netinet/in.h>
61
54
55#include <netinet/in.h>
56
57#include <errno.h>
62#include <stdio.h>
63#include <stdlib.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <kvm.h>
64#include "netstat.h"
65
61#include "netstat.h"
62
66static void unixdomainpr __P((struct socket *, caddr_t));
63static void unixdomainpr __P((struct xunpcb *, struct xsocket *));
67
64
68static struct file *file, *fileNFILE;
69static int nfiles;
70extern kvm_t *kvmd;
65static const char *const socktype[] =
66 { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
71
72void
67
68void
73unixpr(off)
74 u_long off;
69unixpr()
75{
70{
76 register struct file *fp;
77 struct socket sock, *so = &sock;
78 char *filebuf;
79 struct protosw *unixsw = (struct protosw *)off;
71 char *buf;
72 int type;
73 size_t len;
74 struct xsocket *so;
75 struct xunpgen *xug, *oxug;
76 struct xunpcb *xunp;
77 char mibvar[sizeof "net.local.seqpacket.pcblist"];
80
78
81 filebuf = (char *)kvm_getfiles(kvmd, KERN_FILE, 0, &nfiles);
82 if (filebuf == 0) {
83 printf("Out of memory (file table).\n");
84 return;
85 }
86 file = (struct file *)(filebuf + sizeof(fp));
87 fileNFILE = file + nfiles;
88 for (fp = file; fp < fileNFILE; fp++) {
89 if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET)
79 for (type = SOCK_STREAM; type < SOCK_SEQPACKET; type++) {
80 sprintf(mibvar, "net.local.%s.pcblist", socktype[type]);
81
82 len = 0;
83 if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
84 if (errno != ENOENT)
85 warn("sysctl: %s", mibvar);
90 continue;
86 continue;
91 if (kread((u_long)fp->f_data, (char *)so, sizeof (*so)))
92 continue;
93 /* kludge */
94 if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2)
95 if (so->so_pcb)
96 unixdomainpr(so, fp->f_data);
87 }
88 if ((buf = malloc(len)) == 0) {
89 warn("malloc %lu bytes", (u_long)len);
90 return;
91 }
92 if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
93 warn("sysctl: %s", mibvar);
94 free(buf);
95 return;
96 }
97
98 oxug = xug = (struct xunpgen *)buf;
99 for (xug = (struct xunpgen *)((char *)xug + xug->xug_len);
100 xug->xug_len > sizeof(struct xunpgen);
101 xug = (struct xunpgen *)((char *)xug + xug->xug_len)) {
102 xunp = (struct xunpcb *)xug;
103 so = &xunp->xu_socket;
104
105 /* Ignore PCBs which were freed during copyout. */
106 if (xunp->xu_unp.unp_gencnt > oxug->xug_gen)
107 continue;
108 unixdomainpr(xunp, so);
109 }
110 if (xug != oxug && xug->xug_gen != oxug->xug_gen) {
111 if (oxug->xug_count > xug->xug_count) {
112 printf("Some %s sockets may have been deleted.\n",
113 socktype[type]);
114 } else if (oxug->xug_count < xug->xug_count) {
115 printf("Some %s sockets may have been created.\n",
116 socktype[type]);
117 } else {
118 printf("Some %s sockets may have been created or deleted",
119 socktype[type]);
120 }
121 }
122 free(buf);
97 }
98}
99
123 }
124}
125
100static char *socktype[] =
101 { "#0", "stream", "dgram", "raw", "rdm", "seqpacket" };
102
103static void
126static void
104unixdomainpr(so, soaddr)
105 register struct socket *so;
106 caddr_t soaddr;
127unixdomainpr(xunp, so)
128 struct xunpcb *xunp;
129 struct xsocket *so;
107{
130{
108 struct unpcb unpcb, *unp = &unpcb;
109 struct sockaddr_un s_un, *sa = NULL;
131 struct unpcb *unp;
132 struct sockaddr_un *sa;
110 static int first = 1;
111
133 static int first = 1;
134
112 if (kread((u_long)so->so_pcb, (char *)unp, sizeof (*unp)))
113 return;
114 if (unp->unp_addr) {
115 sa = &s_un;
116 if (kread((u_long)unp->unp_addr, (char *)sa, sizeof *sa))
117 sa = (struct sockaddr_un *)0;
118 } else
135 unp = &xunp->xu_unp;
136 if (unp->unp_addr)
137 sa = &xunp->xu_addr;
138 else
119 sa = (struct sockaddr_un *)0;
139 sa = (struct sockaddr_un *)0;
140
120 if (first) {
121 printf("Active UNIX domain sockets\n");
122 printf(
123"%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
124 "Address", "Type", "Recv-Q", "Send-Q",
125 "Inode", "Conn", "Refs", "Nextref");
126 first = 0;
127 }
141 if (first) {
142 printf("Active UNIX domain sockets\n");
143 printf(
144"%-8.8s %-6.6s %-6.6s %-6.6s %8.8s %8.8s %8.8s %8.8s Addr\n",
145 "Address", "Type", "Recv-Q", "Send-Q",
146 "Inode", "Conn", "Refs", "Nextref");
147 first = 0;
148 }
128 printf("%8x %-6.6s %6ld %6ld %8x %8x %8x %8x",
129 (int)soaddr, socktype[so->so_type], so->so_rcv.sb_cc, so->so_snd.sb_cc,
130 (int)unp->unp_vnode, (int)unp->unp_conn,
131 (int)unp->unp_refs, (int)unp->unp_nextref);
149 printf("%8lx %-6.6s %6ld %6ld %8lx %8lx %8lx %8lx",
150 (long)so->so_pcb, socktype[so->so_type], so->so_rcv.sb_cc,
151 so->so_snd.sb_cc,
152 (long)unp->unp_vnode, (long)unp->unp_conn,
153 (long)unp->unp_refs.lh_first, (long)unp->unp_reflink.le_next);
132 if (sa)
133 printf(" %.*s", sa->sun_len, sa->sun_path);
134 putchar('\n');
135}
154 if (sa)
155 printf(" %.*s", sa->sun_len, sa->sun_path);
156 putchar('\n');
157}