From: Evan Hunt Date: Thu, 4 Jun 2020 21:54:36 +0000 (-0700) Subject: make isc_nmsocket_{attach,detach}{} functions private X-Git-Tag: v9.17.3~49^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9e740cad21972194c063f9e471a7ba7d910ee01f;p=thirdparty%2Fbind9.git make isc_nmsocket_{attach,detach}{} functions private there is no need for a caller to reference-count socket objects. they need tto be able tto close listener sockets (i.e., those returned by isc_nm_listen{udp,tcp,tcpdns}), and an isc_nmsocket_close() function has been added for that. other sockets are only accessed via handles. --- diff --git a/lib/isc/include/isc/netmgr.h b/lib/isc/include/isc/netmgr.h index 0c3f3adc7fb..52262d58578 100644 --- a/lib/isc/include/isc/netmgr.h +++ b/lib/isc/include/isc/netmgr.h @@ -57,26 +57,13 @@ isc_nm_closedown(isc_nm_t *mgr); int isc_nm_tid(void); -/* - * isc_nm_freehandle frees a handle, releasing resources - */ -void -isc_nm_freehandle(isc_nmhandle_t *handle); - -void -isc_nmsocket_attach(isc_nmsocket_t *sock, isc_nmsocket_t **target); -/*%< - * isc_nmsocket_attach attaches to a socket, increasing refcount - */ - -void -isc_nmsocket_close(isc_nmsocket_t *sock); - void -isc_nmsocket_detach(isc_nmsocket_t **socketp); +isc_nmsocket_close(isc_nmsocket_t **sockp); /*%< - * isc_nmsocket_detach detaches from socket, decreasing refcount - * and possibly destroying the socket if it's no longer referenced. + * isc_nmsocket_close() detaches a listening socket that was + * created by isc_nm_listenudp(), isc_nm_listentcp(), or + * isc_nm_listentcpdns(). Once there are no remaining child + * sockets with active handles, the socket will be closed. */ void diff --git a/lib/isc/netmgr/netmgr-int.h b/lib/isc/netmgr/netmgr-int.h index b15e6fac290..981f4aa635f 100644 --- a/lib/isc/netmgr/netmgr-int.h +++ b/lib/isc/netmgr/netmgr-int.h @@ -615,6 +615,19 @@ isc__nmsocket_init(isc_nmsocket_t *sock, isc_nm_t *mgr, isc_nmsocket_type type, * and its interface to 'iface'. */ +void +isc__nmsocket_attach(isc_nmsocket_t *sock, isc_nmsocket_t **target); +/*%< + * Attach to a socket, increasing refcount + */ + +void +isc__nmsocket_detach(isc_nmsocket_t **socketp); +/*%< + * Detach from socket, decreasing refcount and possibly destroying the + * socket if it's no longer referenced. + */ + void isc__nmsocket_prep_destroy(isc_nmsocket_t *sock); /*%< diff --git a/lib/isc/netmgr/netmgr.c b/lib/isc/netmgr/netmgr.c index 35469b6dff1..dd589788924 100644 --- a/lib/isc/netmgr/netmgr.c +++ b/lib/isc/netmgr/netmgr.c @@ -686,7 +686,7 @@ isc__nmsocket_active(isc_nmsocket_t *sock) { } void -isc_nmsocket_attach(isc_nmsocket_t *sock, isc_nmsocket_t **target) { +isc__nmsocket_attach(isc_nmsocket_t *sock, isc_nmsocket_t **target) { REQUIRE(VALID_NMSOCK(sock)); REQUIRE(target != NULL && *target == NULL); @@ -878,7 +878,7 @@ isc__nmsocket_prep_destroy(isc_nmsocket_t *sock) { } void -isc_nmsocket_detach(isc_nmsocket_t **sockp) { +isc__nmsocket_detach(isc_nmsocket_t **sockp) { REQUIRE(sockp != NULL && *sockp != NULL); REQUIRE(VALID_NMSOCK(*sockp)); @@ -901,6 +901,17 @@ isc_nmsocket_detach(isc_nmsocket_t **sockp) { } } +void +isc_nmsocket_close(isc_nmsocket_t **sockp) { + REQUIRE(sockp != NULL); + REQUIRE(VALID_NMSOCK(*sockp)); + REQUIRE((*sockp)->type == isc_nm_udplistener || + (*sockp)->type == isc_nm_tcplistener || + (*sockp)->type == isc_nm_tcpdnslistener); + + isc__nmsocket_detach(sockp); +} + void isc__nmsocket_init(isc_nmsocket_t *sock, isc_nm_t *mgr, isc_nmsocket_type type, isc_nmiface_t *iface) { @@ -1171,7 +1182,7 @@ isc_nmhandle_unref(isc_nmhandle_t *handle) { * be deleted by another thread while we're deactivating the * handle. */ - isc_nmsocket_attach(sock, &tmp); + isc__nmsocket_attach(sock, &tmp); nmhandle_deactivate(sock, handle); /* @@ -1189,13 +1200,13 @@ isc_nmhandle_unref(isc_nmhandle_t *handle) { * The socket will be finally detached by the closecb * event handler. */ - isc_nmsocket_attach(sock, &event->sock); + isc__nmsocket_attach(sock, &event->sock); isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid], (isc__netievent_t *)event); } } - isc_nmsocket_detach(&tmp); + isc__nmsocket_detach(&tmp); } void * @@ -1262,7 +1273,7 @@ isc__nm_uvreq_get(isc_nm_t *mgr, isc_nmsocket_t *sock) { *req = (isc__nm_uvreq_t){ .magic = 0 }; req->uv_req.req.data = req; - isc_nmsocket_attach(sock, &req->sock); + isc__nmsocket_attach(sock, &req->sock); req->magic = UVREQ_MAGIC; return (req); @@ -1299,7 +1310,7 @@ isc__nm_uvreq_put(isc__nm_uvreq_t **req0, isc_nmsocket_t *sock) { isc_nmhandle_unref(handle); } - isc_nmsocket_detach(&sock); + isc__nmsocket_detach(&sock); } isc_result_t @@ -1388,7 +1399,7 @@ isc__nm_async_closecb(isc__networker_t *worker, isc__netievent_t *ev0) { UNUSED(worker); ievent->sock->closehandle_cb(ievent->sock); - isc_nmsocket_detach(&ievent->sock); + isc__nmsocket_detach(&ievent->sock); } static void diff --git a/lib/isc/netmgr/tcp.c b/lib/isc/netmgr/tcp.c index 9710f8464f2..5a6ac45735c 100644 --- a/lib/isc/netmgr/tcp.c +++ b/lib/isc/netmgr/tcp.c @@ -209,7 +209,7 @@ isc_nm_listentcp(isc_nm_t *mgr, isc_nmiface_t *iface, isc_nm_cb_t cb, return (ISC_R_SUCCESS); } else { isc_result_t result = nsock->result; - isc_nmsocket_detach(&nsock); + isc__nmsocket_detach(&nsock); return (result); } } @@ -379,14 +379,14 @@ isc__nm_async_tcpchildaccept(isc__networker_t *worker, isc__netievent_t *ev0) { goto error; } - isc_nmsocket_attach(ssock, &csock->server); + isc__nmsocket_attach(ssock, &csock->server); handle = isc__nmhandle_get(csock, NULL, &local); INSIST(ssock->rcb.accept != NULL); csock->read_timeout = ssock->mgr->init; ssock->rcb.accept(handle, ISC_R_SUCCESS, ssock->rcbarg); - isc_nmsocket_detach(&csock); + isc__nmsocket_detach(&csock); return; error: @@ -405,7 +405,7 @@ error: /* * Detach the socket properly to make sure uv_close() is called. */ - isc_nmsocket_detach(&csock); + isc__nmsocket_detach(&csock); } void @@ -416,7 +416,7 @@ isc__nm_tcp_stoplistening(isc_nmsocket_t *sock) { REQUIRE(!isc__nm_in_netthread()); ievent = isc__nm_get_ievent(sock->mgr, netievent_tcpstop); - isc_nmsocket_attach(sock, &ievent->sock); + isc__nmsocket_attach(sock, &ievent->sock); isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid], (isc__netievent_t *)ievent); } @@ -462,7 +462,7 @@ tcp_listenclose_cb(uv_handle_t *handle) { sock->pquota = NULL; UNLOCK(&sock->lock); - isc_nmsocket_detach(&sock); + isc__nmsocket_detach(&sock); } static void @@ -697,7 +697,7 @@ isc__nm_async_tcpaccept(isc__networker_t *worker, isc__netievent_t *ev0) { /* * The socket was attached just before we called isc_quota_attach_cb(). */ - isc_nmsocket_detach(&ievent->sock); + isc__nmsocket_detach(&ievent->sock); } /* @@ -741,7 +741,7 @@ accept_connection(isc_nmsocket_t *ssock, isc_quota_t *quota) { * we need to - but we risk a race then.) */ isc_nmsocket_t *tsock = NULL; - isc_nmsocket_attach(ssock, &tsock); + isc__nmsocket_attach(ssock, &tsock); result = isc_quota_attach_cb(ssock->pquota, "a, &ssock->quotacb); if (result == ISC_R_QUOTA) { @@ -754,7 +754,7 @@ accept_connection(isc_nmsocket_t *ssock, isc_quota_t *quota) { * We're under quota, so there's no need to wait; * Detach the socket. */ - isc_nmsocket_detach(&tsock); + isc__nmsocket_detach(&tsock); } isc__nm_incstats(ssock->mgr, ssock->statsindex[STATID_ACCEPT]); @@ -915,7 +915,7 @@ timer_close_cb(uv_handle_t *uvhandle) { REQUIRE(VALID_NMSOCK(sock)); - isc_nmsocket_detach(&sock->server); + isc__nmsocket_detach(&sock->server); uv_close(&sock->uv_handle.handle, tcp_close_cb); } @@ -933,7 +933,7 @@ tcp_close_direct(isc_nmsocket_t *sock) { uv_close((uv_handle_t *)&sock->timer, timer_close_cb); } else { if (sock->server != NULL) { - isc_nmsocket_detach(&sock->server); + isc__nmsocket_detach(&sock->server); } uv_close(&sock->uv_handle.handle, tcp_close_cb); } diff --git a/lib/isc/netmgr/tcpdns.c b/lib/isc/netmgr/tcpdns.c index 7e3d1728dd2..0f7819fc231 100644 --- a/lib/isc/netmgr/tcpdns.c +++ b/lib/isc/netmgr/tcpdns.c @@ -82,7 +82,7 @@ static void timer_close_cb(uv_handle_t *handle) { isc_nmsocket_t *sock = (isc_nmsocket_t *)uv_handle_get_data(handle); INSIST(VALID_NMSOCK(sock)); - isc_nmsocket_detach(&sock); + isc__nmsocket_detach(&sock); } static void @@ -122,8 +122,8 @@ dnslisten_acceptcb(isc_nmhandle_t *handle, isc_result_t result, void *cbarg) { handle->sock->iface); dnssock->extrahandlesize = dnslistensock->extrahandlesize; - isc_nmsocket_attach(dnslistensock, &dnssock->listener); - isc_nmsocket_attach(handle->sock, &dnssock->outer); + isc__nmsocket_attach(dnslistensock, &dnssock->listener); + isc__nmsocket_attach(handle->sock, &dnssock->outer); dnssock->peer = handle->sock->peer; dnssock->read_timeout = handle->sock->mgr->init; dnssock->tid = isc_nm_tid(); @@ -310,7 +310,7 @@ isc_nm_listentcpdns(isc_nm_t *mgr, isc_nmiface_t *iface, isc_nm_recv_cb_t cb, return (ISC_R_SUCCESS); } else { atomic_store(&dnslistensock->closed, true); - isc_nmsocket_detach(&dnslistensock); + isc__nmsocket_detach(&dnslistensock); return (result); } } @@ -327,7 +327,7 @@ isc__nm_tcpdns_stoplistening(isc_nmsocket_t *sock) { if (sock->outer != NULL) { isc_nm_stoplistening(sock->outer); - isc_nmsocket_detach(&sock->outer); + isc__nmsocket_detach(&sock->outer); } } @@ -517,10 +517,10 @@ tcpdns_close_direct(isc_nmsocket_t *sock) { */ if (sock->outer != NULL) { sock->outer->rcb.recv = NULL; - isc_nmsocket_detach(&sock->outer); + isc__nmsocket_detach(&sock->outer); } if (sock->listener != NULL) { - isc_nmsocket_detach(&sock->listener); + isc__nmsocket_detach(&sock->listener); } atomic_store(&sock->closed, true); } diff --git a/lib/isc/netmgr/udp.c b/lib/isc/netmgr/udp.c index 066e5edcb57..bd40fa38119 100644 --- a/lib/isc/netmgr/udp.c +++ b/lib/isc/netmgr/udp.c @@ -152,7 +152,8 @@ isc__nm_async_udplisten(isc__networker_t *worker, isc__netievent_t *ev0) { #endif uv_udp_init_ex(&worker->loop, &sock->uv_handle.udp, uv_init_flags); uv_handle_set_data(&sock->uv_handle.handle, NULL); - isc_nmsocket_attach(sock, (isc_nmsocket_t **)&sock->uv_handle.udp.data); + isc__nmsocket_attach(sock, + (isc_nmsocket_t **)&sock->uv_handle.udp.data); r = uv_udp_open(&sock->uv_handle.udp, sock->fd); if (r == 0) { @@ -186,7 +187,7 @@ udp_close_cb(uv_handle_t *handle) { isc_nmsocket_t *sock = uv_handle_get_data(handle); atomic_store(&sock->closed, true); - isc_nmsocket_detach((isc_nmsocket_t **)&sock->uv_handle.udp.data); + isc__nmsocket_detach((isc_nmsocket_t **)&sock->uv_handle.udp.data); } static void diff --git a/lib/isc/win32/libisc.def.in b/lib/isc/win32/libisc.def.in index 681b730731b..4a7ec68056e 100644 --- a/lib/isc/win32/libisc.def.in +++ b/lib/isc/win32/libisc.def.in @@ -464,7 +464,7 @@ isc_nm_tcp_settimeouts isc_nm_tcpdns_keepalive isc_nm_tcpdns_sequential isc_nm_tid -isc_nmsocket_detach +isc_nmsocket_close isc__nm_acquire_interlocked isc__nm_drop_interlocked isc__nm_acquire_interlocked_force diff --git a/lib/ns/interfacemgr.c b/lib/ns/interfacemgr.c index 633485f6478..30ecbcda29f 100644 --- a/lib/ns/interfacemgr.c +++ b/lib/ns/interfacemgr.c @@ -553,11 +553,11 @@ void ns_interface_shutdown(ns_interface_t *ifp) { if (ifp->udplistensocket != NULL) { isc_nm_stoplistening(ifp->udplistensocket); - isc_nmsocket_detach(&ifp->udplistensocket); + isc_nmsocket_close(&ifp->udplistensocket); } if (ifp->tcplistensocket != NULL) { isc_nm_stoplistening(ifp->tcplistensocket); - isc_nmsocket_detach(&ifp->tcplistensocket); + isc_nmsocket_close(&ifp->tcplistensocket); } if (ifp->clientmgr != NULL) { ns_clientmgr_destroy(&ifp->clientmgr);