diff options
author | Max Wipfli <mail@maxwipfli.ch> | 2021-06-07 20:26:09 +0200 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-06-09 17:43:32 +0430 |
commit | 4efccbd030b06753aa97993571180dba9b4e6a12 (patch) | |
tree | 43aa2ba605a258d7f1a7b60a97d52bc79c691afc /Userland/Services/LookupServer/LookupServer.cpp | |
parent | 7b39db3bf401bb067a3fd239e9341830f3185bad (diff) | |
download | serenity-4efccbd030b06753aa97993571180dba9b4e6a12.zip |
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.
Diffstat (limited to 'Userland/Services/LookupServer/LookupServer.cpp')
-rw-r--r-- | Userland/Services/LookupServer/LookupServer.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Userland/Services/LookupServer/LookupServer.cpp b/Userland/Services/LookupServer/LookupServer.cpp index 692b885d9a..f0e294e908 100644 --- a/Userland/Services/LookupServer/LookupServer.cpp +++ b/Userland/Services/LookupServer/LookupServer.cpp @@ -45,6 +45,29 @@ LookupServer::LookupServer() 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")) { m_dns_server = DNSServer::construct(this); // TODO: drop root privileges here. @@ -68,6 +91,7 @@ LookupServer::LookupServer() void LookupServer::load_etc_hosts() { + m_etc_hosts.clear(); auto add_answer = [this](const DNSName& name, DNSRecordType record_type, String data) { auto it = m_etc_hosts.find(name); if (it == m_etc_hosts.end()) { |