From a4d378030df61760ea4daa2ddbe46b1366f8e977 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Wed, 21 Feb 2018 11:08:23 -0800 Subject: Fixes for POSIX pollq structure. It was possible for pollq arm to be called on a node that was removed in some circumstances -- particularly and ep that was closed in the callback. While here, lets use normal booleans for closed state, and only call the arm function (which is not free -- typicall it involves a mutex and may even involve a system call) if we are going to arm some events. We also initialize these things properly, and clean up a stale comment. This work is done to faciliate the kqueue work by @liamstask. --- src/platform/posix/posix_epdesc.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/platform/posix/posix_epdesc.c') diff --git a/src/platform/posix/posix_epdesc.c b/src/platform/posix/posix_epdesc.c index 5b1d6a9c..3da36fa2 100644 --- a/src/platform/posix/posix_epdesc.c +++ b/src/platform/posix/posix_epdesc.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -36,7 +37,7 @@ struct nni_posix_epdesc { nni_posix_pollq_node node; nni_list connectq; nni_list acceptq; - int closed; + bool closed; struct sockaddr_storage locaddr; struct sockaddr_storage remaddr; socklen_t loclen; @@ -131,9 +132,6 @@ nni_posix_epdesc_doaccept(nni_posix_epdesc *ed) int newfd; while ((aio = nni_list_first(&ed->acceptq)) != NULL) { -// We could argue that knowing the remote peer address would -// be nice. But frankly if someone wants it, they can just -// do getpeername(). #ifdef NNG_USE_ACCEPT4 newfd = accept4(ed->node.fd, NULL, NULL, SOCK_CLOEXEC); @@ -201,7 +199,7 @@ nni_posix_epdesc_doclose(nni_posix_epdesc *ed) struct sockaddr_un *sun; int fd; - ed->closed = 1; + ed->closed = true; while ((aio = nni_list_first(&ed->acceptq)) != NULL) { nni_posix_epdesc_finish(aio, NNG_ECLOSED, 0); } @@ -250,7 +248,9 @@ nni_posix_epdesc_cb(void *arg) if (!nni_list_empty(&ed->acceptq)) { events |= POLLIN; } - nni_posix_pollq_arm(&ed->node, events); + if ((!ed->closed) && (events != 0)) { + nni_posix_pollq_arm(&ed->node, events); + } nni_mtx_unlock(&ed->mtx); } @@ -433,6 +433,8 @@ nni_posix_epdesc_init(nni_posix_epdesc **edp) ed->node.index = 0; ed->node.cb = nni_posix_epdesc_cb; ed->node.data = ed; + ed->node.fd = -1; + ed->closed = false; nni_aio_list_init(&ed->connectq); nni_aio_list_init(&ed->acceptq); -- cgit v1.2.3-70-g09d2