-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added means to reconfigure the running instance of nettracer.
Added an --args_file <path> arg to load configuration from file rather than cmd line args. The configuration is reloaded live when the config file is updated by means of rename(2)/mv(1). For this purpose, IN_MOVED_TO event is configured by means of inotify_add_watch(2). The args file format is boost::program_options::parse_config_file() compatible. Signed-off-by: Lukasz Lasek <[email protected]>
- Loading branch information
Showing
10 changed files
with
314 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "config_watcher.h" | ||
|
||
#include <filesystem> | ||
#include <functional> | ||
#include <stdexcept> | ||
#include <system_error> | ||
|
||
using namespace std::literals::string_literals; | ||
|
||
void config_watcher::init(std::string file_path) { | ||
if (iw_token) { | ||
throw std::logic_error("config_watcher already initialized"); | ||
} | ||
if (file_path.empty()) { | ||
return; | ||
} | ||
|
||
std::filesystem::path full_path(file_path); | ||
dir_path = full_path.parent_path(); | ||
file_name = full_path.filename(); | ||
iw_token = iw.watch_path(dir_path, IN_MOVED_TO, std::bind(&config_watcher::on_event, this, std::placeholders::_1)); | ||
if (!iw_token) { | ||
throw std::system_error(errno, std::system_category(), dir_path.c_str()); | ||
} | ||
} | ||
|
||
config_watcher::operator bool() { | ||
return iw_token; | ||
} | ||
|
||
int config_watcher::get_poll_fd() { | ||
return iw.get_poll_fd(); | ||
} | ||
|
||
void config_watcher::on_pollin() { | ||
if (!iw_token) { | ||
return; | ||
} | ||
iw.on_pollin(); | ||
} | ||
|
||
void config_watcher::on_event(inotify_event& ie) { | ||
if (!(ie.mask & IN_MOVED_TO)) { | ||
return; | ||
} | ||
if (!ie.len) { | ||
return; | ||
} | ||
if (ie.wd != iw_token.wd) { | ||
return; | ||
} | ||
if (file_name != ie.name) { | ||
return; | ||
} | ||
|
||
config_changed = true; | ||
} | ||
|
||
bool config_watcher::is_config_changed() { | ||
return config_changed; | ||
} | ||
|
||
void config_watcher::reset() { | ||
config_changed = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include "inotify_watcher.h" | ||
|
||
#include <boost/noncopyable.hpp> | ||
#include <filesystem> | ||
#include <functional> | ||
#include <string> | ||
|
||
class config_watcher : private boost::noncopyable { | ||
public: | ||
config_watcher() = default; | ||
~config_watcher() = default; | ||
|
||
void init(std::string file_path); | ||
|
||
operator bool(); | ||
int get_poll_fd(); | ||
|
||
void on_pollin(); | ||
|
||
bool is_config_changed(); | ||
|
||
void reset(); | ||
|
||
protected: | ||
void on_event(inotify_event& ie); | ||
|
||
bool config_changed{false}; | ||
inotify_watcher iw{}; | ||
inotify_watcher::watch_token iw_token{}; | ||
std::filesystem::path dir_path{}; | ||
std::filesystem::path file_name{}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "inotify_watcher.h" | ||
|
||
#include <system_error> | ||
#include <unistd.h> | ||
#include <utility> | ||
|
||
inotify_watcher::inotify_watcher() { | ||
fd = ::inotify_init1(IN_NONBLOCK | IN_CLOEXEC); | ||
if (fd == -1) { | ||
throw std::system_error(errno, std::system_category(), "inotify_init failed"); | ||
} | ||
} | ||
|
||
inotify_watcher::~inotify_watcher() { | ||
for (auto& wd : wds) { | ||
unwatch_path({wd.first}); | ||
} | ||
::close(fd); | ||
} | ||
|
||
inotify_watcher::watch_token inotify_watcher::watch_path(std::string path, uint32_t inotify_mask, watch_callback wc) { | ||
if (fd == -1) { | ||
return {-1}; | ||
} | ||
int wd = ::inotify_add_watch(fd, path.c_str(), inotify_mask); | ||
if (wd == -1) { | ||
return {-1}; | ||
} | ||
wds.insert(std::make_pair(wd, wc)); | ||
return {wd}; | ||
} | ||
|
||
bool inotify_watcher::unwatch_path(watch_token token) { | ||
if (!token) { | ||
return false; | ||
} | ||
if (fd == -1) { | ||
return false; | ||
} | ||
if (wds.find(token.wd) == wds.end()) { | ||
return false; | ||
} | ||
int rc = ::inotify_rm_watch(fd, token.wd); | ||
if (rc == -1) { | ||
return false; | ||
} | ||
wds.erase(token.wd); | ||
return true; | ||
} | ||
|
||
inotify_watcher::operator bool() { | ||
return fd != -1; | ||
} | ||
|
||
int inotify_watcher::get_poll_fd() { | ||
return fd; | ||
} | ||
|
||
void inotify_watcher::on_pollin() { | ||
inotify_event events[64]; | ||
while (true) { | ||
auto len = ::read(fd, &events, sizeof(events)); | ||
if (len == -1) { | ||
break; | ||
} | ||
dispatch(events, len / sizeof(*events)); | ||
} | ||
} | ||
|
||
void inotify_watcher::dispatch(inotify_event* events, size_t count) { | ||
while (count) { | ||
auto wd = wds.find(events->wd); | ||
if (wd != wds.end()) { | ||
wd->second(*events); | ||
} | ||
|
||
++events; | ||
--count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <map> | ||
#include <string> | ||
#include <sys/inotify.h> | ||
|
||
class inotify_watcher { | ||
public: | ||
using watch_callback = std::function<void(inotify_event&)>; | ||
|
||
struct watch_token { | ||
operator bool() { return wd != -1; }; | ||
int wd{-1}; | ||
}; | ||
|
||
inotify_watcher(); | ||
~inotify_watcher(); | ||
|
||
watch_token watch_path(std::string path, uint32_t inotify_mask, watch_callback wc); | ||
bool unwatch_path(watch_token token); | ||
|
||
operator bool(); | ||
int get_poll_fd(); | ||
|
||
void on_pollin(); | ||
|
||
protected: | ||
void dispatch(inotify_event* events, size_t count); | ||
|
||
// inotify fd | ||
int fd{-1}; | ||
|
||
// inotify watch descriptors | ||
std::map<int, watch_callback> wds; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.