From 22185bade8dbd0cf222f6208a1bd0de96f116b7d Mon Sep 17 00:00:00 2001 From: glebius Date: Fri, 2 Apr 2010 11:07:55 +0000 Subject: [PATCH] Merge r205082, r205083 that fix 'netstat -f netgraph' functionality. git-svn-id: svn://svn.freebsd.org/base/stable/8@206088 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- sys/netgraph/ng_socket.c | 21 +++++++++++++++++++++ sys/netgraph/ng_socketvar.h | 1 + usr.bin/netstat/netgraph.c | 6 +++--- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/sys/netgraph/ng_socket.c b/sys/netgraph/ng_socket.c index af68c636d..28e4f0e22 100644 --- a/sys/netgraph/ng_socket.c +++ b/sys/netgraph/ng_socket.c @@ -156,6 +156,11 @@ static u_long ngpdg_recvspace = 20 * 1024; SYSCTL_INT(_net_graph, OID_AUTO, recvspace, CTLFLAG_RW, &ngpdg_recvspace , 0, "Maximum space for incoming Netgraph datagrams"); +/* List of all sockets (for netstat -f netgraph) */ +static LIST_HEAD(, ngpcb) ngsocklist; + +static struct mtx ngsocketlist_mtx; + #define sotongpcb(so) ((struct ngpcb *)(so)->so_pcb) /* If getting unexplained errors returned, set this to "kdb_enter("X"); */ @@ -547,6 +552,9 @@ ng_attach_cntl(struct socket *so) return (error); } + /* Store a hint for netstat(1). */ + priv->node_id = priv->node->nd_ID; + /* Link the node and the private data. */ NG_NODE_SET_PRIVATE(priv->node, priv); NG_NODE_REF(priv->node); @@ -584,6 +592,10 @@ ng_attach_common(struct socket *so, int type) so->so_pcb = (caddr_t)pcbp; pcbp->ng_socket = so; + /* Add the socket to linked list */ + mtx_lock(&ngsocketlist_mtx); + LIST_INSERT_HEAD(&ngsocklist, pcbp, socks); + mtx_unlock(&ngsocketlist_mtx); return (0); } @@ -617,6 +629,9 @@ ng_detach_common(struct ngpcb *pcbp, int which) } pcbp->ng_socket->so_pcb = NULL; + mtx_lock(&ngsocketlist_mtx); + LIST_REMOVE(pcbp, socks); + mtx_unlock(&ngsocketlist_mtx); free(pcbp, M_PCB); } @@ -1115,8 +1130,14 @@ ngs_mod_event(module_t mod, int event, void *data) switch (event) { case MOD_LOAD: + mtx_init(&ngsocketlist_mtx, "ng_socketlist", NULL, MTX_DEF); break; case MOD_UNLOAD: + /* Ensure there are no open netgraph sockets. */ + if (!LIST_EMPTY(&ngsocklist)) { + error = EBUSY; + break; + } #ifdef NOTYET /* Unregister protocol domain XXX can't do this yet.. */ #endif diff --git a/sys/netgraph/ng_socketvar.h b/sys/netgraph/ng_socketvar.h index 3cf810355..c1e59dcf8 100644 --- a/sys/netgraph/ng_socketvar.h +++ b/sys/netgraph/ng_socketvar.h @@ -61,6 +61,7 @@ struct ngsock { int refs; struct mtx mtx; /* mtx to wait on */ int error; /* place to store error */ + ng_ID_t node_id; /* a hint for netstat(1) to find the node */ }; #define NGS_FLAG_NOLINGER 1 /* close with last hook */ diff --git a/usr.bin/netstat/netgraph.c b/usr.bin/netstat/netgraph.c index c4dd647eb..d51041446 100644 --- a/usr.bin/netstat/netgraph.c +++ b/usr.bin/netstat/netgraph.c @@ -166,14 +166,14 @@ netgraphprotopr(u_long off, const char *name, int af1 __unused, name, sockb.so_rcv.sb_cc, sockb.so_snd.sb_cc); /* Get ngsock structure */ - if (ngpcb.sockdata == 0) /* unconnected data socket */ + if (ngpcb.sockdata == NULL) /* unconnected data socket */ goto finish; kread((u_long)ngpcb.sockdata, (char *)&info, sizeof(info)); /* Get info on associated node */ - if (info.node == 0 || csock == -1) + if (info.node_id == 0 || csock == -1) goto finish; - snprintf(path, sizeof(path), "[%lx]:", (u_long) info.node); + snprintf(path, sizeof(path), "[%x]:", info.node_id); if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_NODEINFO, NULL, 0) < 0) goto finish; -- 2.45.0