diff --git a/src/DNS_Mgr.cc b/src/DNS_Mgr.cc index 903485a652..93eba847cc 100644 --- a/src/DNS_Mgr.cc +++ b/src/DNS_Mgr.cc @@ -440,9 +440,14 @@ void DNS_Mgr::InitSource() } if ( nb_dns ) - iosource_mgr->RegisterFd(nb_dns_fd(nb_dns), this); + { + if ( ! iosource_mgr->RegisterFd(nb_dns_fd(nb_dns), this) ) + reporter->FatalError("Failed to register nb_dns file descriptor with iosource_mgr"); + } else + { reporter->Warning("problem initializing NB-DNS: %s", err); + } did_init = true; } @@ -1490,5 +1495,5 @@ void DNS_Mgr::GetStats(Stats* stats) void DNS_Mgr::Terminate() { if ( nb_dns ) - iosource_mgr->UnregisterFd(nb_dns_fd(nb_dns)); + iosource_mgr->UnregisterFd(nb_dns_fd(nb_dns), this); } diff --git a/src/broker/Manager.cc b/src/broker/Manager.cc index a7ad3a1df6..1e54ce33a0 100644 --- a/src/broker/Manager.cc +++ b/src/broker/Manager.cc @@ -207,16 +207,18 @@ void Manager::InitPostScript() auto cqs = get_option("Broker::congestion_queue_size")->AsCount(); bstate = std::make_shared(std::move(config), cqs); - iosource_mgr->RegisterFd(bstate->subscriber.fd(), this); - iosource_mgr->RegisterFd(bstate->status_subscriber.fd(), this); + if ( ! iosource_mgr->RegisterFd(bstate->subscriber.fd(), this) ) + reporter->FatalError("Failed to register broker subscriber with iosource_mgr"); + if ( ! iosource_mgr->RegisterFd(bstate->status_subscriber.fd(), this) ) + reporter->FatalError("Failed to register broker status subscriber with iosource_mgr"); } void Manager::Terminate() { FlushLogBuffers(); - iosource_mgr->UnregisterFd(bstate->subscriber.fd()); - iosource_mgr->UnregisterFd(bstate->status_subscriber.fd()); + iosource_mgr->UnregisterFd(bstate->subscriber.fd(), this); + iosource_mgr->UnregisterFd(bstate->status_subscriber.fd(), this); vector stores_to_close; @@ -1503,7 +1505,7 @@ bool Manager::CloseStore(const string& name) if ( s == data_stores.end() ) return false; - iosource_mgr->UnregisterFd(s->second->proxy.mailbox().descriptor()); + iosource_mgr->UnregisterFd(s->second->proxy.mailbox().descriptor(), this); for ( auto i = pending_queries.begin(); i != pending_queries.end(); ) if ( i->second->Store().name() == name ) diff --git a/src/iosource/Manager.cc b/src/iosource/Manager.cc index c5791a2272..66b2beac5f 100644 --- a/src/iosource/Manager.cc +++ b/src/iosource/Manager.cc @@ -22,12 +22,13 @@ using namespace iosource; Manager::WakeupHandler::WakeupHandler() { - iosource_mgr->RegisterFd(flare.FD(), this); + if ( ! iosource_mgr->RegisterFd(flare.FD(), this) ) + reporter->FatalError("Failed to register WakeupHandler's fd with iosource_mgr"); } Manager::WakeupHandler::~WakeupHandler() { - iosource_mgr->UnregisterFd(flare.FD()); + iosource_mgr->UnregisterFd(flare.FD(), this); } void Manager::WakeupHandler::Process() @@ -207,7 +208,7 @@ void Manager::ConvertTimeout(double timeout, struct timespec& spec) } } -void Manager::RegisterFd(int fd, IOSource* src) +bool Manager::RegisterFd(int fd, IOSource* src) { struct kevent event; EV_SET(&event, fd, EVFILT_READ, EV_ADD, 0, 0, NULL); @@ -219,14 +220,16 @@ void Manager::RegisterFd(int fd, IOSource* src) fd_map[fd] = src; Wakeup("RegisterFd"); + return true; } else { - DBG_LOG(DBG_MAINLOOP, "Failed to register fd %d from %s: %s", fd, src->Tag(), strerror(errno)); + reporter->Error("Failed to register fd %d from %s: %s", fd, src->Tag(), strerror(errno)); + return false; } } -void Manager::UnregisterFd(int fd) +bool Manager::UnregisterFd(int fd, IOSource* src) { if ( fd_map.find(fd) != fd_map.end() ) { @@ -234,11 +237,17 @@ void Manager::UnregisterFd(int fd) EV_SET(&event, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); int ret = kevent(event_queue, &event, 1, NULL, 0, NULL); if ( ret != -1 ) - DBG_LOG(DBG_MAINLOOP, "Unregistered fd %d", fd); + DBG_LOG(DBG_MAINLOOP, "Unregistered fd %d from %s", fd, src->Tag()); fd_map.erase(fd); Wakeup("UnregisterFd"); + return true; + } + else + { + reporter->Error("Attempted to unregister an unknown file descriptor %d from %s", fd, src->Tag()); + return false; } } diff --git a/src/iosource/Manager.h b/src/iosource/Manager.h index ddf89db11a..1cd8eb965d 100644 --- a/src/iosource/Manager.h +++ b/src/iosource/Manager.h @@ -113,12 +113,12 @@ public: * checked for readiness. * @param src The IOSource that owns the file descriptor. */ - void RegisterFd(int fd, IOSource* src); + bool RegisterFd(int fd, IOSource* src); /** * Unregisters a file descriptor from the FindReadySources checks. */ - void UnregisterFd(int fd); + bool UnregisterFd(int fd, IOSource* src); /** * Forces the poll in FindReadySources to wake up immediately. This method diff --git a/src/supervisor/Supervisor.cc b/src/supervisor/Supervisor.cc index 4ef342ffb7..124c9b7771 100644 --- a/src/supervisor/Supervisor.cc +++ b/src/supervisor/Supervisor.cc @@ -358,8 +358,11 @@ void Supervisor::HandleChildSignal() void Supervisor::InitPostScript() { iosource_mgr->Register(this); - iosource_mgr->RegisterFd(signal_flare.FD(), this); - iosource_mgr->RegisterFd(stem_pipe->InFD(), this); + + if ( ! iosource_mgr->RegisterFd(signal_flare.FD(), this) ) + reporter->FatalError("Failed registration for signal_flare with iosource_mgr"); + if ( ! iosource_mgr->RegisterFd(stem_pipe->InFD(), this) ) + reporter->FatalError("Failed registration for stem_pipe with iosource_mgr"); } double Supervisor::GetNextTimeout() diff --git a/src/threading/MsgThread.cc b/src/threading/MsgThread.cc index 41267147d0..a1aac9ea15 100644 --- a/src/threading/MsgThread.cc +++ b/src/threading/MsgThread.cc @@ -181,7 +181,8 @@ MsgThread::MsgThread() : BasicThread(), queue_in(this, 0), queue_out(0, this) failed = false; thread_mgr->AddMsgThread(this); - iosource_mgr->RegisterFd(flare.FD(), this); + if ( ! iosource_mgr->RegisterFd(flare.FD(), this) ) + reporter->FatalError("Failed to register MsgThread fd with iosource_mgr"); SetClosed(false); } @@ -190,7 +191,7 @@ MsgThread::~MsgThread() { // Unregister this thread from the iosource manager so it doesn't wake // up the main poll anymore. - iosource_mgr->UnregisterFd(flare.FD()); + iosource_mgr->UnregisterFd(flare.FD(), this); } // Set by Bro's main signal handler.