From 6c8e7031b24d47c55a651609f5d00f8e809e81fe Mon Sep 17 00:00:00 2001 From: SlawekNowy Date: Tue, 27 May 2025 14:35:48 +0200 Subject: [PATCH 1/2] Add initial support for 5.1 flags. --- include/notify-cpp/event.h | 15 +++++++++- source/event.cpp | 58 ++++++++++++++++++++++++++++++++++++-- source/fanotify.cpp | 10 +++++++ 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/include/notify-cpp/event.h b/include/notify-cpp/event.h index 29bffd1..f53ef51 100644 --- a/include/notify-cpp/event.h +++ b/include/notify-cpp/event.h @@ -46,8 +46,17 @@ enum class Event { delete_self = (1 << 10), move_self = (1 << 11), + + // undefined behaver none = (1 << 12), + //fanotify only. + q_overflow = (1 << 13), + open_perm = (1 << 14), + ondir = (1 << 15), + on_child = (1 << 16), + + // helper close = Event::close_write | Event::close_nowrite, @@ -87,7 +96,7 @@ static const std::array AllFanFlags = {{FAN_ACCESS, FAN_ALL_CLASS_BITS, FAN_ENABLE_AUDIT}}; #endif -static const std::array AllEvents = {Event::access, +static const std::array AllEvents = {Event::access, Event::modify, Event::attrib, Event::close_write, @@ -101,6 +110,10 @@ static const std::array AllEvents = {Event::access, Event::move_self, Event::close, Event::move, + Event::q_overflow, + Event::open_perm, + Event::ondir, + Event::on_child, Event::all}; template <> diff --git a/source/event.cpp b/source/event.cpp index 567eb27..78c221e 100644 --- a/source/event.cpp +++ b/source/event.cpp @@ -25,6 +25,7 @@ #include #include +#include #include @@ -84,6 +85,12 @@ EventHandler::getInotifyEvent(const Event e) const return IN_ALL_EVENTS; case Event::none: return 0; + case Event::q_overflow: + case Event::open_perm: + case Event::on_child: + case Event::ondir: + assert(!"None existing event"); + return 0; } return 0; } @@ -96,16 +103,23 @@ EventHandler::getFanotifyEvent(const Event e) const return FAN_ACCESS; case Event::modify: return FAN_MODIFY; +#if LINUX_VERSION_CODE < KERNEL_VERSION(5,1,0) case Event::attrib: assert(!"None existing event"); return 0; + case Event::attrib: + return FAN_ATTRIB; +#else + case Event::attrib: + return FAN_ATTRIB; +#endif case Event::close_write: return FAN_CLOSE_WRITE; case Event::close_nowrite: return FAN_CLOSE_NOWRITE; case Event::open: return FAN_OPEN; - +#if LINUX_VERSION_CODE < KERNEL_VERSION(5,1,0) case Event::moved_from: case Event::moved_to: case Event::create: @@ -114,15 +128,37 @@ EventHandler::getFanotifyEvent(const Event e) const case Event::move_self: assert(!"None existing event"); return 0; - +#else + case Event::moved_from: + return FAN_MOVED_FROM; + case Event::moved_to: + return FAN_MOVED_TO; + case Event::create: + return FAN_CREATE; + case Event::delete_sub: + return FAN_DELETE; + case Event::delete_self: + return FAN_DELETE_SELF; + case Event::move_self: + return FAN_MOVE_SELF; +#endif case Event::close: return FAN_CLOSE; - +#if LINUX_VERSION_CODE < KERNEL_VERSION(5,1,0) case Event::move: case Event::all: case Event::none: assert(!"None existing event"); return 0; + +#else + case Event::move: + return FAN_MOVE; + case Event::all: //doesn't exist by itself, but its constituents are. Do we emulate this? + case Event::none: + assert(!"None existing event"); + return 0; +#endif } assert(!"None existing event"); return 0; @@ -165,6 +201,14 @@ toString(const Event event) return std::string("all"); case Event::none: return std::string("none"); + case Event::q_overflow: + return std::string("queue_overflow"); + case Event::open_perm: + return std::string("open_perm"); + case Event::ondir: + return std::string("ondir"); + case Event::on_child: + return std::string("on_child"); } assert(!"None existing event"); return std::string("ERROR"); @@ -298,6 +342,14 @@ Event EventHandler::getFanotify(std::uint32_t e) const return Event::open; case FAN_CLOSE: return Event::close; + case FAN_Q_OVERFLOW: + return Event::q_overflow; + case FAN_OPEN_PERM: + return Event::open_perm; + case FAN_ONDIR: + return Event::ondir; + case FAN_EVENT_ON_CHILD: + return Event::on_child; /* TODO case FAN_Q_OVERFLOW: case FAN_OPEN_PERM: diff --git a/source/fanotify.cpp b/source/fanotify.cpp index ebfb74e..2e8f9dd 100644 --- a/source/fanotify.cpp +++ b/source/fanotify.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -105,6 +106,15 @@ void Fanotify::watchFile(const FileSystemEvent& fse) void Fanotify::watch(const std::filesystem::path& path, unsigned int flags, const Event event) { + //FAN_EVENT_ON_CHILD is ignored with FAN_MARK_MOUNT. Notify the user on this. + if( ((flags & FAN_MARK_MOUNT)!=0) && ((event & Event::on_child) == Event::on_child)) + { + std::stringstream errorStream; + errorStream << "watchMountPoint: Event::on_child has no effect on the mountpoint!'"; + // + //throw std::runtime_error(errorStream.str()); + } + /* Add new fanotify mark */ if (fanotify_mark(_FanotifyFd, flags, getEventMask(event), AT_FDCWD, path.c_str()) < 0) { std::stringstream errorStream; From 1f8ebc3b96f7ba23b9201ad4d1290e6d21e4a300 Mon Sep 17 00:00:00 2001 From: SlawekNowy <38943477+SlawekNowy@users.noreply.github.com> Date: Tue, 3 Jun 2025 10:14:03 +0200 Subject: [PATCH 2/2] Update event.cpp Fixed a small typo. --- source/event.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/event.cpp b/source/event.cpp index 78c221e..3c0525d 100644 --- a/source/event.cpp +++ b/source/event.cpp @@ -107,8 +107,6 @@ EventHandler::getFanotifyEvent(const Event e) const case Event::attrib: assert(!"None existing event"); return 0; - case Event::attrib: - return FAN_ATTRIB; #else case Event::attrib: return FAN_ATTRIB;