This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 218fa03ecff71522a1b7a3f9c1dbd12988e58720
Author: p-szafonimateusz <[email protected]>
AuthorDate: Mon Aug 24 15:36:44 2026 +0200

    fs/vfs: Support event injection through the inotify descriptor
    
    With CONFIG_FS_NOTIFY_INJECT, one write() of a struct notify_inject_s
    (event mask + NUL terminated absolute path) to an inotify file
    descriptor injects one file change notification.  Intended for user
    file system daemons that must report spontaneous changes, and for
    testing.  Default n: any writer can forge events for any path.
    
    Signed-off-by: p-szafonimateusz <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 Documentation/os/filesystem/inotify.rst |  8 ++++++
 fs/vfs/Kconfig                          |  9 ++++++
 fs/vfs/fs_inotify.c                     | 49 +++++++++++++++++++++++++++++++++
 include/sys/inotify.h                   | 10 +++++++
 4 files changed, 76 insertions(+)

diff --git a/Documentation/os/filesystem/inotify.rst 
b/Documentation/os/filesystem/inotify.rst
index b6e3a6bf613..3dc4a1f0c8d 100644
--- a/Documentation/os/filesystem/inotify.rst
+++ b/Documentation/os/filesystem/inotify.rst
@@ -153,3 +153,11 @@ backing store with:
 While a path has a file system watch handle, locally generated events
 on it are suppressed: all events for that path come from the file
 system, so each operation is reported once.
+
+Event injection
+---------------
+
+With ``CONFIG_FS_NOTIFY_INJECT``, one ``write()`` of a
+``struct notify_inject_s`` (event mask + NUL terminated absolute path)
+to an inotify file descriptor injects one notification.  Off by
+default since any writer can forge events for any path.
diff --git a/fs/vfs/Kconfig b/fs/vfs/Kconfig
index 2afb1c276ad..2abb907a405 100644
--- a/fs/vfs/Kconfig
+++ b/fs/vfs/Kconfig
@@ -107,6 +107,15 @@ config FS_NOTIFY_FD_POLLWAITERS
        int "Max pollwaiters in one notify device"
        default 2
 
+config FS_NOTIFY_INJECT
+       bool
+       default n
+       ---help---
+               Allow injecting file change notifications by writing a
+               struct notify_inject_s to an inotify file descriptor.  Selected
+               by file systems whose user space server reports spontaneous
+               changes (for example userfs).  Any writer can forge events.
+
 endif # FS_NOTIFY
 
 config FS_BACKTRACE
diff --git a/fs/vfs/fs_inotify.c b/fs/vfs/fs_inotify.c
index bb2a38f41fe..f8266f48239 100644
--- a/fs/vfs/fs_inotify.c
+++ b/fs/vfs/fs_inotify.c
@@ -127,6 +127,10 @@ static int     inotify_open(FAR struct file *filep);
 static int     inotify_close(FAR struct file *filep);
 static ssize_t inotify_read(FAR struct file *filep, FAR char *buffer,
                             size_t buflen);
+#ifdef CONFIG_FS_NOTIFY_INJECT
+static ssize_t inotify_write(FAR struct file *filep,
+                             FAR const char *buffer, size_t buflen);
+#endif
 static int     inotify_poll(FAR struct file *filep, FAR struct pollfd_s *fds,
                             bool setup);
 static int     inotify_ioctl(FAR struct file *filep, int cmd,
@@ -145,7 +149,11 @@ static const struct file_operations g_inotify_fops =
   inotify_open,     /* open */
   inotify_close,    /* close */
   inotify_read,     /* read */
+#ifdef CONFIG_FS_NOTIFY_INJECT
+  inotify_write,    /* write */
+#else
   NULL,             /* write */
+#endif
   NULL,             /* seek */
   inotify_ioctl,    /* ioctl */
   NULL,             /* mmap */
@@ -580,6 +588,42 @@ static int inotify_open(FAR struct file *filep)
   return OK;
 }
 
+/****************************************************************************
+ * Name: inotify_write
+ *
+ * Description:
+ *   Inject one event per write of a struct notify_inject_s payload
+ *   (event mask + NUL terminated absolute path).
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_NOTIFY_INJECT
+static ssize_t inotify_write(FAR struct file *filep,
+                             FAR const char *buffer, size_t buflen)
+{
+  FAR const struct notify_inject_s *event;
+  size_t hdrlen = offsetof(struct notify_inject_s, path);
+  size_t pathlen;
+
+  event = (FAR const struct notify_inject_s *)buffer;
+  if (event == NULL || buflen <= hdrlen || buflen > hdrlen + PATH_MAX)
+    {
+      return -EINVAL;
+    }
+
+  pathlen = strnlen(event->path, buflen - hdrlen);
+  if (pathlen == buflen - hdrlen || event->path[0] != '/')
+    {
+      /* Not NUL terminated within the write, or not an absolute path */
+
+      return -EINVAL;
+    }
+
+  notify_event(NULL, event->path, event->mask);
+  return buflen;
+}
+#endif
+
 /****************************************************************************
  * Name: inotify_poll
  *
@@ -1493,8 +1537,13 @@ int inotify_init1(int flags)
       goto exit_set_errno;
     }
 
+#ifdef CONFIG_FS_NOTIFY_INJECT
+  ret = file_allocate_from_inode(&g_inotify_inode, O_RDWR | flags,
+                                 0, dev, 0);
+#else
   ret = file_allocate_from_inode(&g_inotify_inode, O_RDONLY | flags,
                                  0, dev, 0);
+#endif
   if (ret < 0)
     {
       ferr("Failed to allocate inotify fd\n");
diff --git a/include/sys/inotify.h b/include/sys/inotify.h
index e0107631c6f..31a86e40cee 100644
--- a/include/sys/inotify.h
+++ b/include/sys/inotify.h
@@ -83,6 +83,16 @@ struct inotify_event
   char     name[0];         /* Stub for possible name */
 };
 
+/* Payload written to an inotify file descriptor (CONFIG_FS_NOTIFY_INJECT):
+ * one write injects one file change notification.
+ */
+
+struct notify_inject_s
+{
+  uint32_t mask;            /* inotify event mask */
+  char     path[1];         /* NUL terminated absolute path */
+};
+
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/

Reply via email to