mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:57:35 +00:00
LookupServer: Watch /etc/hosts for changes during runtime
This adds a FileWatcher to the LookupServer which watches '/etc/hosts' for changes during runtime and reloads its contents. If the file is deleted, m_etc_hosts will be cleared. Since we now need to access '/etc/hosts' later during runtime, it needs to be unveiled with read permissions.
This commit is contained in:
parent
7b39db3bf4
commit
4efccbd030
3 changed files with 31 additions and 0 deletions
|
@ -45,6 +45,29 @@ LookupServer::LookupServer()
|
||||||
|
|
||||||
load_etc_hosts();
|
load_etc_hosts();
|
||||||
|
|
||||||
|
auto maybe_file_watcher = Core::FileWatcher::create();
|
||||||
|
// NOTE: If this happens during startup, something is very wrong.
|
||||||
|
if (maybe_file_watcher.is_error()) {
|
||||||
|
dbgln("Core::FileWatcher::create(): {}", maybe_file_watcher.error());
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
m_file_watcher = maybe_file_watcher.release_value();
|
||||||
|
|
||||||
|
m_file_watcher->on_change = [this](auto&) {
|
||||||
|
dbgln("Reloading '/etc/hosts' because it was changed.");
|
||||||
|
load_etc_hosts();
|
||||||
|
};
|
||||||
|
|
||||||
|
auto result = m_file_watcher->add_watch("/etc/hosts", Core::FileWatcherEvent::Type::ContentModified | Core::FileWatcherEvent::Type::Deleted);
|
||||||
|
// NOTE: If this happens during startup, something is very wrong.
|
||||||
|
if (result.is_error()) {
|
||||||
|
dbgln("Core::FileWatcher::add_watch(): {}", result.error());
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
} else if (!result.value()) {
|
||||||
|
dbgln("Core::FileWatcher::add_watch(): {}", result.value());
|
||||||
|
VERIFY_NOT_REACHED();
|
||||||
|
}
|
||||||
|
|
||||||
if (config->read_bool_entry("DNS", "EnableServer")) {
|
if (config->read_bool_entry("DNS", "EnableServer")) {
|
||||||
m_dns_server = DNSServer::construct(this);
|
m_dns_server = DNSServer::construct(this);
|
||||||
// TODO: drop root privileges here.
|
// TODO: drop root privileges here.
|
||||||
|
@ -68,6 +91,7 @@ LookupServer::LookupServer()
|
||||||
|
|
||||||
void LookupServer::load_etc_hosts()
|
void LookupServer::load_etc_hosts()
|
||||||
{
|
{
|
||||||
|
m_etc_hosts.clear();
|
||||||
auto add_answer = [this](const DNSName& name, DNSRecordType record_type, String data) {
|
auto add_answer = [this](const DNSName& name, DNSRecordType record_type, String data) {
|
||||||
auto it = m_etc_hosts.find(name);
|
auto it = m_etc_hosts.find(name);
|
||||||
if (it == m_etc_hosts.end()) {
|
if (it == m_etc_hosts.end()) {
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include "DNSPacket.h"
|
#include "DNSPacket.h"
|
||||||
#include "DNSServer.h"
|
#include "DNSServer.h"
|
||||||
#include "MulticastDNS.h"
|
#include "MulticastDNS.h"
|
||||||
|
#include <LibCore/FileWatcher.h>
|
||||||
#include <LibCore/Object.h>
|
#include <LibCore/Object.h>
|
||||||
|
|
||||||
namespace LookupServer {
|
namespace LookupServer {
|
||||||
|
@ -35,6 +36,7 @@ private:
|
||||||
RefPtr<DNSServer> m_dns_server;
|
RefPtr<DNSServer> m_dns_server;
|
||||||
RefPtr<MulticastDNS> m_mdns;
|
RefPtr<MulticastDNS> m_mdns;
|
||||||
Vector<String> m_nameservers;
|
Vector<String> m_nameservers;
|
||||||
|
RefPtr<Core::FileWatcher> m_file_watcher;
|
||||||
HashMap<DNSName, Vector<DNSAnswer>, DNSName::Traits> m_etc_hosts;
|
HashMap<DNSName, Vector<DNSAnswer>, DNSName::Traits> m_etc_hosts;
|
||||||
HashMap<DNSName, Vector<DNSAnswer>, DNSName::Traits> m_lookup_cache;
|
HashMap<DNSName, Vector<DNSAnswer>, DNSName::Traits> m_lookup_cache;
|
||||||
};
|
};
|
||||||
|
|
|
@ -30,6 +30,11 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (unveil("/etc/hosts", "r") < 0) {
|
||||||
|
perror("unveil");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (unveil(nullptr, nullptr) < 0) {
|
if (unveil(nullptr, nullptr) < 0) {
|
||||||
perror("unveil");
|
perror("unveil");
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue