Add better error messaging when RegisterFd/UnregisterFd fail

This commit is contained in:
Tim Wojtulewicz 2020-01-30 13:34:10 -07:00
parent 0cfb115c1b
commit fea0339aca
6 changed files with 39 additions and 19 deletions

View file

@ -440,9 +440,14 @@ void DNS_Mgr::InitSource()
} }
if ( nb_dns ) 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 else
{
reporter->Warning("problem initializing NB-DNS: %s", err); reporter->Warning("problem initializing NB-DNS: %s", err);
}
did_init = true; did_init = true;
} }
@ -1490,5 +1495,5 @@ void DNS_Mgr::GetStats(Stats* stats)
void DNS_Mgr::Terminate() void DNS_Mgr::Terminate()
{ {
if ( nb_dns ) if ( nb_dns )
iosource_mgr->UnregisterFd(nb_dns_fd(nb_dns)); iosource_mgr->UnregisterFd(nb_dns_fd(nb_dns), this);
} }

View file

@ -207,16 +207,18 @@ void Manager::InitPostScript()
auto cqs = get_option("Broker::congestion_queue_size")->AsCount(); auto cqs = get_option("Broker::congestion_queue_size")->AsCount();
bstate = std::make_shared<BrokerState>(std::move(config), cqs); bstate = std::make_shared<BrokerState>(std::move(config), cqs);
iosource_mgr->RegisterFd(bstate->subscriber.fd(), this); if ( ! iosource_mgr->RegisterFd(bstate->subscriber.fd(), this) )
iosource_mgr->RegisterFd(bstate->status_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() void Manager::Terminate()
{ {
FlushLogBuffers(); FlushLogBuffers();
iosource_mgr->UnregisterFd(bstate->subscriber.fd()); iosource_mgr->UnregisterFd(bstate->subscriber.fd(), this);
iosource_mgr->UnregisterFd(bstate->status_subscriber.fd()); iosource_mgr->UnregisterFd(bstate->status_subscriber.fd(), this);
vector<string> stores_to_close; vector<string> stores_to_close;
@ -1503,7 +1505,7 @@ bool Manager::CloseStore(const string& name)
if ( s == data_stores.end() ) if ( s == data_stores.end() )
return false; 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(); ) for ( auto i = pending_queries.begin(); i != pending_queries.end(); )
if ( i->second->Store().name() == name ) if ( i->second->Store().name() == name )

View file

@ -22,12 +22,13 @@ using namespace iosource;
Manager::WakeupHandler::WakeupHandler() 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() Manager::WakeupHandler::~WakeupHandler()
{ {
iosource_mgr->UnregisterFd(flare.FD()); iosource_mgr->UnregisterFd(flare.FD(), this);
} }
void Manager::WakeupHandler::Process() 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; struct kevent event;
EV_SET(&event, fd, EVFILT_READ, EV_ADD, 0, 0, NULL); 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; fd_map[fd] = src;
Wakeup("RegisterFd"); Wakeup("RegisterFd");
return true;
} }
else 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() ) 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); EV_SET(&event, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
int ret = kevent(event_queue, &event, 1, NULL, 0, NULL); int ret = kevent(event_queue, &event, 1, NULL, 0, NULL);
if ( ret != -1 ) 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); fd_map.erase(fd);
Wakeup("UnregisterFd"); Wakeup("UnregisterFd");
return true;
}
else
{
reporter->Error("Attempted to unregister an unknown file descriptor %d from %s", fd, src->Tag());
return false;
} }
} }

View file

@ -113,12 +113,12 @@ public:
* checked for readiness. * checked for readiness.
* @param src The IOSource that owns the file descriptor. * @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. * 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 * Forces the poll in FindReadySources to wake up immediately. This method

View file

@ -358,8 +358,11 @@ void Supervisor::HandleChildSignal()
void Supervisor::InitPostScript() void Supervisor::InitPostScript()
{ {
iosource_mgr->Register(this); 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() double Supervisor::GetNextTimeout()

View file

@ -181,7 +181,8 @@ MsgThread::MsgThread() : BasicThread(), queue_in(this, 0), queue_out(0, this)
failed = false; failed = false;
thread_mgr->AddMsgThread(this); 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); SetClosed(false);
} }
@ -190,7 +191,7 @@ MsgThread::~MsgThread()
{ {
// Unregister this thread from the iosource manager so it doesn't wake // Unregister this thread from the iosource manager so it doesn't wake
// up the main poll anymore. // up the main poll anymore.
iosource_mgr->UnregisterFd(flare.FD()); iosource_mgr->UnregisterFd(flare.FD(), this);
} }
// Set by Bro's main signal handler. // Set by Bro's main signal handler.