1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#include "utils/platform/unix/interface.h"
#include "utils/platform/platform.h"
#include <functional>
#include <thread>
#include <array>
#include <cstring>
#include <csignal>
#include <ctime>
#include <unistd.h>
#include <fcntl.h>
#include <sys/event.h>
#include <iostream>
namespace TangoTest::platform
{
std::vector<std::string> default_env()
{
return {"PATH="};
}
namespace unix
{
struct FileWatcher::Impl
{
explicit Impl(const char *filename)
{
// Create a kqueue that allows us to watch file events.
if((kernel_queue = kqueue()) < 0)
{
throw_strerror("Could not open a kernel queue.");
}
// Open a file that we want to watch via the kqueue events.
const auto fd{open(filename, O_EVTONLY)};
if(fd <= 0)
{
throw_strerror("Could not open file \"", filename, "\".");
}
else
{
event_fd = fd;
}
// Tell the kqueue event set what we want to monitor.
wanted_events = NOTE_WRITE;
EV_SET64(&(events_to_monitor[0]), event_fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, wanted_events, 0, 0, 0, 0);
if(::pipe(watched_fds) != 0)
{
throw_strerror("Could not open a pipe for communication with the "
"testing framework.");
}
}
void start_watching()
{
stop_thread = false;
event_thread = std::thread(&FileWatcher::Impl::thread_run, this);
}
void thread_run()
{
struct timespec timeout
{
};
while(!stop_thread)
{
// Always reset the timeout to 0.1s.
timeout.tv_sec = 0;
timeout.tv_nsec = 100'000'000;
// Wait until a kqueue event that we want happens. This call
// will time out after timeout seconds.
const int event_count{kevent64(kernel_queue,
&(events_to_monitor[0]),
number_of_watched_files,
&(event_data[0]),
number_of_watched_files,
KEVENT_FLAG_NONE,
&timeout)};
if((event_count < 0) || (event_data[0].flags == EV_ERROR))
{
// An error occurred.
throw_strerror("An error occurred (event count=", event_count, ").");
break;
}
if(event_count)
{
::write(watched_fds[1], "\0", 1);
}
};
}
void stop_watching_thread()
{
stop_thread = true;
if(event_thread.joinable())
{
event_thread.join();
}
}
void stop_watching()
{
stop_watching_thread();
::close(kernel_queue);
const int fd{event_fd};
event_fd = -1;
::close(fd);
const int fds[2]{watched_fds[0], watched_fds[1]};
watched_fds[0] = watched_fds[1] = -1;
::close(fds[1]);
::close(fds[0]);
}
~Impl()
{
stop_watching();
}
const std::size_t number_of_watched_files{1};
int event_fd{-1};
int watched_fds[2]{-1, -1};
int kernel_queue{-1};
bool stop_thread{true};
std::thread event_thread;
std::array<struct kevent64_s, 1> events_to_monitor{};
std::array<struct kevent64_s, 1> event_data{};
struct timespec timeout
{
1, 0
};
unsigned int wanted_events;
};
FileWatcher::FileWatcher(const char *filename) :
m_pimpl{std::make_unique<FileWatcher::Impl>(filename)}
{
}
FileWatcher::~FileWatcher() { }
int FileWatcher::get_file_descriptor()
{
return m_pimpl->watched_fds[0];
}
void FileWatcher::start_watching()
{
m_pimpl->start_watching();
}
void FileWatcher::stop_watching()
{
m_pimpl->stop_watching();
}
void FileWatcher::cleanup_in_child()
{
m_pimpl.reset(nullptr);
}
void FileWatcher::pop_event()
{
char foo;
::read(m_pimpl->watched_fds[0], &foo, 1);
}
// There is no equivalent of prctl(PR_SET_PDEATHSIG). It is not worth the
// effort to implement something that mimics it. The tests works without it.
void kill_self_on_parent_death([[maybe_unused]] pid_t ppid) { }
} // namespace unix
} // namespace TangoTest::platform
|