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 51e4c6823d64e3845330326612f9e5ac3a77bc60
Author: p-szafonimateusz <[email protected]>
AuthorDate: Mon Aug 24 15:37:17 2026 +0200

    fs/userfs: Add userfs_notify_event injection helper
    
    Let a user file system daemon report spontaneous changes to watched
    files: the helper injects one notification by writing the event to an
    inotify descriptor (CONFIG_FS_NOTIFY_INJECT).
    
    Signed-off-by: p-szafonimateusz <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 Documentation/os/filesystem/userfs.rst | 10 ++++++
 fs/userfs/Kconfig                      |  1 +
 include/nuttx/fs/userfs.h              | 20 +++++++++++
 libs/libc/userfs/lib_userfs.c          | 64 ++++++++++++++++++++++++++++++++++
 4 files changed, 95 insertions(+)

diff --git a/Documentation/os/filesystem/userfs.rst 
b/Documentation/os/filesystem/userfs.rst
index 40cebe5fbf5..399d34904ca 100644
--- a/Documentation/os/filesystem/userfs.rst
+++ b/Documentation/os/filesystem/userfs.rst
@@ -3,3 +3,13 @@ User file system
 ================
 
 See ``include/nuttx/fs/userfs.h``.
+
+Advisory locking and inotify
+============================
+
+The daemon may implement the optional ``getlk``/``setlk`` methods to
+arbitrate advisory locks itself (``setlk`` is non-blocking; return
+``-EAGAIN`` on conflict) and the ``notify`` method to learn the watch
+life cycle.  Without them the OS local lock table is used.  Events are
+injected back with ``userfs_notify_event()``
+(``CONFIG_FS_NOTIFY_INJECT``).
diff --git a/fs/userfs/Kconfig b/fs/userfs/Kconfig
index f02476b5bbe..682b1cfab11 100644
--- a/fs/userfs/Kconfig
+++ b/fs/userfs/Kconfig
@@ -28,6 +28,7 @@ config FS_USERFS
        bool "User file system"
        default n
        depends on NET_IPv4 && NET_UDP && NET_LOOPBACK
+       select FS_NOTIFY_INJECT if FS_NOTIFY
        ---help---
                Enable support for user file system.  See 
include/nuttx/fs/userfs.h
 
diff --git a/include/nuttx/fs/userfs.h b/include/nuttx/fs/userfs.h
index ade47b61593..1d436941f01 100644
--- a/include/nuttx/fs/userfs.h
+++ b/include/nuttx/fs/userfs.h
@@ -686,6 +686,26 @@ int userfs_run(FAR const char *mountpt,
                FAR const struct userfs_operations_s *userops,
                FAR void *volinfo, size_t mxwrite);
 
+/****************************************************************************
+ * Name: userfs_notify_event
+ *
+ * Description:
+ *   Inject a file change notification through an inotify descriptor
+ *   (CONFIG_FS_NOTIFY_INJECT).
+ *
+ * Input Parameters:
+ *   abspath - Absolute path of the affected object
+ *   mask    - inotify event mask
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_NOTIFY_INJECT
+int userfs_notify_event(FAR const char *abspath, uint32_t mask);
+#endif
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libs/libc/userfs/lib_userfs.c b/libs/libc/userfs/lib_userfs.c
index 90e11f30567..7a99467fe54 100644
--- a/libs/libc/userfs/lib_userfs.c
+++ b/libs/libc/userfs/lib_userfs.c
@@ -34,14 +34,18 @@
 #include <stdio.h>
 #include <string.h>
 #include <fcntl.h>
+#include <limits.h>
+#include <unistd.h>
 #include <assert.h>
 #include <errno.h>
 #include <nuttx/debug.h>
 
 #include <arpa/inet.h>
+#include <sys/inotify.h>
 #include <netinet/in.h>
 
 #include <nuttx/fs/userfs.h>
+#include <nuttx/lib/lib.h>
 #include <nuttx/mutex.h>
 
 #include "libc.h"
@@ -1332,3 +1336,63 @@ errout_with_info:
   lib_free(info);
   return ret;
 }
+
+/****************************************************************************
+ * Name: userfs_notify_event
+ *
+ * Description:
+ *   Inject a file change notification through an inotify descriptor
+ *   (CONFIG_FS_NOTIFY_INJECT).
+ *
+ * Input Parameters:
+ *   abspath - Absolute path of the affected object
+ *   mask    - inotify event mask
+ *
+ * Returned Value:
+ *   Zero is returned on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_NOTIFY_INJECT
+int userfs_notify_event(FAR const char *abspath, uint32_t mask)
+{
+  FAR struct notify_inject_s *event;
+  size_t pathlen;
+  size_t len;
+  ssize_t nwritten;
+  int fd;
+
+  if (abspath == NULL || abspath[0] != '/')
+    {
+      return -EINVAL;
+    }
+
+  pathlen = strlen(abspath) + 1;
+  len     = sizeof(uint32_t) + pathlen;
+  if (len > PATH_MAX)
+    {
+      return -ENAMETOOLONG;
+    }
+
+  event = lib_get_tempbuffer(PATH_MAX);
+  if (event == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  event->mask = mask;
+  memcpy(event->path, abspath, pathlen);
+
+  fd = inotify_init1(0);
+  if (fd < 0)
+    {
+      lib_put_tempbuffer(event);
+      return -get_errno();
+    }
+
+  nwritten = write(fd, event, len);
+  close(fd);
+  lib_put_tempbuffer(event);
+  return nwritten < 0 ? -get_errno() : OK;
+}
+#endif /* CONFIG_FS_NOTIFY_INJECT */

Reply via email to