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

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

commit 420648b0c67397ed1886aec0a8bf2d442e034603
Author: zhanghongyu <[email protected]>
AuthorDate: Fri Aug 23 15:03:57 2024 +0800

    drivers/pipes: add fcntl(F_SETPIPE_SZ/F_GETPIPE_SZ) support
    
    allows user programs to modify pipe size, but not larger than
    CONFIG_DEV_PIPE_MAXSIZE
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 drivers/pipes/pipe_common.c | 27 +++++++++++++++++++++++++++
 fs/vfs/fs_fcntl.c           | 20 ++++++++++++++++++++
 include/fcntl.h             |  2 ++
 include/nuttx/fs/ioctl.h    |  8 ++++++++
 4 files changed, 57 insertions(+)

diff --git a/drivers/pipes/pipe_common.c b/drivers/pipes/pipe_common.c
index efb95027ca..0d8fb24b6e 100644
--- a/drivers/pipes/pipe_common.c
+++ b/drivers/pipes/pipe_common.c
@@ -27,6 +27,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
+#include <sys/param.h>
 #include <stdint.h>
 #include <stdbool.h>
 #include <stdlib.h>
@@ -849,6 +850,32 @@ int pipecommon_ioctl(FAR struct file *filep, int cmd, 
unsigned long arg)
         }
         break;
 
+      case PIPEIOC_SETSIZE:
+        {
+          size_t size = (size_t)arg;
+          if (size == 0)
+            {
+              ret = -EINVAL;
+              break;
+            }
+
+          size = MIN(size, CONFIG_DEV_PIPE_MAXSIZE);
+          ret = circbuf_resize(&dev->d_buffer, size);
+          if (ret != 0)
+            {
+              break;
+            }
+
+          dev->d_bufsize = size;
+        }
+        break;
+
+      case PIPEIOC_GETSIZE:
+        {
+          ret = dev->d_bufsize;
+        }
+        break;
+
       case FIONWRITE:  /* Number of bytes waiting in send queue */
       case FIONREAD:   /* Number of bytes available for reading */
         {
diff --git a/fs/vfs/fs_fcntl.c b/fs/vfs/fs_fcntl.c
index d7e7a152a9..c87417376b 100644
--- a/fs/vfs/fs_fcntl.c
+++ b/fs/vfs/fs_fcntl.c
@@ -243,6 +243,26 @@ static int file_vfcntl(FAR struct file *filep, int cmd, 
va_list ap)
           ret = file_ioctl(filep, FIOC_FILEPATH, va_arg(ap, FAR char *));
         }
 
+        break;
+      case F_SETPIPE_SZ:
+        /* Modify the capacity of the pipe to arg bytes, but not larger than
+         * CONFIG_DEV_PIPE_MAXSIZE.
+         */
+
+        {
+          ret = file_ioctl(filep, PIPEIOC_SETSIZE, va_arg(ap, int));
+        }
+
+        break;
+      case F_GETPIPE_SZ:
+
+        /* Return the capacity of the pipe */
+
+        {
+          ret = file_ioctl(filep, PIPEIOC_GETSIZE);
+        }
+
+        break;
       default:
         break;
     }
diff --git a/include/fcntl.h b/include/fcntl.h
index 132c1ebe9e..9e897dba82 100644
--- a/include/fcntl.h
+++ b/include/fcntl.h
@@ -103,6 +103,8 @@
 #define F_ADD_SEALS     16 /* Add the bit-mask argument arg to the set of 
seals of the inode */
 #define F_GET_SEALS     17 /* Get (as the function result) the current set of 
seals of the inode */
 #define F_DUPFD_CLOEXEC 18 /* Duplicate file descriptor with close-on-exit 
set.  */
+#define F_SETPIPE_SZ    19 /* Modify the capacity of the pipe to arg bytes, 
but not larger than CONFIG_DEV_PIPE_MAXSIZE */
+#define F_GETPIPE_SZ    20 /* Return the capacity of the pipe */
 
 /* For posix fcntl() and lockf() */
 
diff --git a/include/nuttx/fs/ioctl.h b/include/nuttx/fs/ioctl.h
index ff5df177e6..825de7dbcf 100644
--- a/include/nuttx/fs/ioctl.h
+++ b/include/nuttx/fs/ioctl.h
@@ -484,6 +484,14 @@
                                                * IN: pipe_peek_s
                                                * OUT: Length of data */
 
+#define PIPEIOC_SETSIZE     _PIPEIOC(0x0005)  /* Pipe get size interface
+                                               * IN: size_t
+                                               * OUT: None */
+
+#define PIPEIOC_GETSIZE     _PIPEIOC(0x0006)  /* Pipe get size interface
+                                               * IN: None
+                                               * OUT: int */
+
 /* RTC driver ioctl definitions *********************************************/
 
 /* (see nuttx/include/rtc.h */

Reply via email to