[PATCH 16/18] libio: LIBIO_GET_IOP() LIBIO_GET_IOP_WITH_ACCESS()

Sebastian Huber sebastian.huber at embedded-brains.de
Wed Sep 13 13:39:49 UTC 2017


Replace rtems_libio_check_fd(), rtems_libio_iop(),
rtems_libio_check_open() and rtems_libio_check_permissions()
combinations with new LIBIO_GET_IOP() and LIBIO_GET_IOP_WITH_ACCESS()
macros.

Update #3082.
---
 cpukit/libcsupport/include/rtems/libio_.h  | 78 +++++++++++++++++-------------
 cpukit/libcsupport/src/close.c             |  4 +-
 cpukit/libcsupport/src/fchdir.c            |  4 +-
 cpukit/libcsupport/src/fchmod.c            |  4 +-
 cpukit/libcsupport/src/fchown.c            |  4 +-
 cpukit/libcsupport/src/fcntl.c             |  9 ++--
 cpukit/libcsupport/src/fdatasync.c         |  5 +-
 cpukit/libcsupport/src/fpathconf.c         |  4 +-
 cpukit/libcsupport/src/fstat.c             |  4 +-
 cpukit/libcsupport/src/fsync.c             |  4 +-
 cpukit/libcsupport/src/ftruncate.c         |  5 +-
 cpukit/libcsupport/src/ioctl.c             |  4 +-
 cpukit/libcsupport/src/lseek.c             |  4 +-
 cpukit/libcsupport/src/read.c              |  5 +-
 cpukit/libcsupport/src/write.c             |  5 +-
 cpukit/libnetworking/rtems/rtems_syscall.c |  3 +-
 16 files changed, 63 insertions(+), 83 deletions(-)

diff --git a/cpukit/libcsupport/include/rtems/libio_.h b/cpukit/libcsupport/include/rtems/libio_.h
index d9ba176ef0..f29d6fa93d 100644
--- a/cpukit/libcsupport/include/rtems/libio_.h
+++ b/cpukit/libcsupport/include/rtems/libio_.h
@@ -137,8 +137,6 @@ static inline uint32_t rtems_libio_iop_flags_clear(
  * @param[in] fd The file descriptor.
  *
  * @return The iop corresponding to the specified file descriptor.
- *
- * @see rtems_libio_check_fd().
  */
 static inline rtems_libio_t *rtems_libio_iop( int fd )
 {
@@ -169,19 +167,50 @@ static inline rtems_libio_t *rtems_libio_iop( int fd )
       }                                              \
   } while (0)
 
-/*
- *  rtems_libio_check_fd
- *
- *  Macro to check if a file descriptor number is valid.
- */
+/**
+ * @brief Macro to get the iop for the specified file descriptor.
+ *
+ * Checks that the file descriptor is in the valid range and open.
+ */
+#define LIBIO_GET_IOP( _fd, _iop ) \
+  do { \
+    uint32_t _flags; \
+    if ( (uint32_t) ( _fd ) >= rtems_libio_number_iops ) { \
+      rtems_set_errno_and_return_minus_one( EBADF ); \
+    } \
+    _iop = rtems_libio_iop( _fd ); \
+    _flags = _iop->flags; \
+    if ( ( _flags & LIBIO_FLAGS_OPEN ) == 0 ) { \
+      rtems_set_errno_and_return_minus_one( EBADF ); \
+    } \
+  } while ( 0 )
 
-#define rtems_libio_check_fd(_fd) \
-  do {                                                     \
-      if ((uint32_t) (_fd) >= rtems_libio_number_iops) {   \
-          errno = EBADF;                                   \
-          return -1;                                       \
-      }                                                    \
-  } while (0)
+/**
+ * @brief Macro to get the iop for the specified file descriptor with access
+ * flags and error.
+ *
+ * Checks that the file descriptor is in the valid range and open.
+ */
+#define LIBIO_GET_IOP_WITH_ACCESS( _fd, _iop, _access_flags, _access_error ) \
+  do { \
+    uint32_t _flags; \
+    uint32_t _mandatory; \
+    if ( (uint32_t) ( _fd ) >= rtems_libio_number_iops ) { \
+      rtems_set_errno_and_return_minus_one( EBADF ); \
+    } \
+    _iop = rtems_libio_iop( _fd ); \
+    _flags = _iop->flags; \
+    _mandatory = LIBIO_FLAGS_OPEN | ( _access_flags ); \
+    if ( ( _flags & _mandatory ) != _mandatory ) { \
+      int _error; \
+      if ( ( _flags & LIBIO_FLAGS_OPEN ) == 0 ) { \
+        _error = EBADF; \
+      } else { \
+        _error = _access_error; \
+      } \
+      rtems_set_errno_and_return_minus_one( _error ); \
+    } \
+  } while ( 0 )
 
 /*
  *  rtems_libio_check_buffer
@@ -210,21 +239,6 @@ static inline rtems_libio_t *rtems_libio_iop( int fd )
       }                                 \
   } while (0)
 
-/*
- *  rtems_libio_check_permissions
- *
- *  Macro to check if a file descriptor is open for this operation.
- *  On failure, return the user specified error.
- */
-
-#define rtems_libio_check_permissions(_iop, _flag, _errno) \
-  do {                                                      \
-      if (((_iop)->flags & (_flag)) == 0) {                 \
-            rtems_set_errno_and_return_minus_one( _errno ); \
-            return -1;                                      \
-      }                                                     \
-  } while (0)
-
 /**
  * @brief Clones a node.
  *
@@ -933,11 +947,7 @@ static inline ssize_t rtems_libio_iovec_eval(
     }
   }
 
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open( iop );
-  rtems_libio_check_permissions( iop, flags, EBADF );
-
+  LIBIO_GET_IOP_WITH_ACCESS( fd, iop, flags, EBADF );
   *iopp = iop;
 
   return total;
diff --git a/cpukit/libcsupport/src/close.c b/cpukit/libcsupport/src/close.c
index 649107dff0..7eaeb64fea 100644
--- a/cpukit/libcsupport/src/close.c
+++ b/cpukit/libcsupport/src/close.c
@@ -27,9 +27,7 @@ int close(
   rtems_libio_t      *iop;
   int                 rc;
 
-  rtems_libio_check_fd(fd);
-  iop = rtems_libio_iop(fd);
-  rtems_libio_check_is_open(iop);
+  LIBIO_GET_IOP( fd, iop );
 
   rtems_libio_iop_flags_clear( iop, LIBIO_FLAGS_OPEN );
 
diff --git a/cpukit/libcsupport/src/fchdir.c b/cpukit/libcsupport/src/fchdir.c
index 6988ccaa6f..ece73ab3ec 100644
--- a/cpukit/libcsupport/src/fchdir.c
+++ b/cpukit/libcsupport/src/fchdir.c
@@ -39,9 +39,7 @@ int fchdir( int fd )
   st.st_uid = 0;
   st.st_gid = 0;
 
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open( iop );
+  LIBIO_GET_IOP( fd, iop );
 
   rtems_filesystem_instance_lock( &iop->pathinfo );
   rv = (*iop->pathinfo.handlers->fstat_h)( &iop->pathinfo, &st );
diff --git a/cpukit/libcsupport/src/fchmod.c b/cpukit/libcsupport/src/fchmod.c
index 1a9122c9bb..126b015501 100644
--- a/cpukit/libcsupport/src/fchmod.c
+++ b/cpukit/libcsupport/src/fchmod.c
@@ -67,9 +67,7 @@ int fchmod( int fd, mode_t mode )
   int rv;
   rtems_libio_t *iop;
 
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open(iop);
+  LIBIO_GET_IOP( fd, iop );
 
   rtems_filesystem_instance_lock( &iop->pathinfo );
 
diff --git a/cpukit/libcsupport/src/fchown.c b/cpukit/libcsupport/src/fchown.c
index 10cd4bce4f..bd787d89e3 100644
--- a/cpukit/libcsupport/src/fchown.c
+++ b/cpukit/libcsupport/src/fchown.c
@@ -64,9 +64,7 @@ int fchown( int fd, uid_t owner, gid_t group )
   int rv;
   rtems_libio_t *iop;
 
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open(iop);
+  LIBIO_GET_IOP( fd, iop );
 
   rtems_filesystem_instance_lock( &iop->pathinfo );
 
diff --git a/cpukit/libcsupport/src/fcntl.c b/cpukit/libcsupport/src/fcntl.c
index 8e509ea269..4da1e3daa6 100644
--- a/cpukit/libcsupport/src/fcntl.c
+++ b/cpukit/libcsupport/src/fcntl.c
@@ -61,7 +61,10 @@ static int duplicate2_iop( rtems_libio_t *iop, int fd2 )
   rtems_libio_t *iop2;
   int            rv = 0;
 
-  rtems_libio_check_fd( fd2 );
+  if ( (uint32_t) fd2 >= rtems_libio_number_iops ) {
+    rtems_set_errno_and_return_minus_one( EBADF );
+  }
+
   iop2 = rtems_libio_iop( fd2 );
 
   if (iop != iop2)
@@ -108,9 +111,7 @@ static int vfcntl(
   int            mask;
   int            ret = 0;
 
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open(iop);
+  LIBIO_GET_IOP( fd, iop );
 
   /*
    *  Now process the fcntl().
diff --git a/cpukit/libcsupport/src/fdatasync.c b/cpukit/libcsupport/src/fdatasync.c
index a5191c3b04..14e66726e6 100644
--- a/cpukit/libcsupport/src/fdatasync.c
+++ b/cpukit/libcsupport/src/fdatasync.c
@@ -29,10 +29,7 @@ int fdatasync(
 {
   rtems_libio_t *iop;
 
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open(iop);
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE, EBADF );
+  LIBIO_GET_IOP_WITH_ACCESS( fd, iop, LIBIO_FLAGS_WRITE, EBADF );
 
   /*
    *  Now process the fdatasync().
diff --git a/cpukit/libcsupport/src/fpathconf.c b/cpukit/libcsupport/src/fpathconf.c
index 3cab4025b7..db323136c7 100644
--- a/cpukit/libcsupport/src/fpathconf.c
+++ b/cpukit/libcsupport/src/fpathconf.c
@@ -36,9 +36,7 @@ long fpathconf(
   rtems_libio_t                          *iop;
   const rtems_filesystem_limits_and_options_t *the_limits;
 
-  rtems_libio_check_fd(fd);
-  iop = rtems_libio_iop(fd);
-  rtems_libio_check_is_open(iop);
+  LIBIO_GET_IOP( fd, iop );
 
   /*
    *  Now process the information request.
diff --git a/cpukit/libcsupport/src/fstat.c b/cpukit/libcsupport/src/fstat.c
index 4c284bd408..4a10d166bd 100644
--- a/cpukit/libcsupport/src/fstat.c
+++ b/cpukit/libcsupport/src/fstat.c
@@ -36,9 +36,7 @@ int fstat(
   /*
    *  Now process the stat() request.
    */
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open(iop);
+  LIBIO_GET_IOP( fd, iop );
 
   /*
    *  Zero out the stat structure so the various support
diff --git a/cpukit/libcsupport/src/fsync.c b/cpukit/libcsupport/src/fsync.c
index 19c04b99d0..6332180721 100644
--- a/cpukit/libcsupport/src/fsync.c
+++ b/cpukit/libcsupport/src/fsync.c
@@ -32,9 +32,7 @@ int fsync(
 {
   rtems_libio_t *iop;
 
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open(iop);
+  LIBIO_GET_IOP( fd, iop );
 
   /*
    *  Now process the fsync().
diff --git a/cpukit/libcsupport/src/ftruncate.c b/cpukit/libcsupport/src/ftruncate.c
index 64c2dc0045..401510b2ff 100644
--- a/cpukit/libcsupport/src/ftruncate.c
+++ b/cpukit/libcsupport/src/ftruncate.c
@@ -29,10 +29,7 @@ int ftruncate( int fd, off_t length )
   if ( length >= 0 ) {
     rtems_libio_t *iop;
 
-    rtems_libio_check_fd( fd );
-    iop = rtems_libio_iop( fd );
-    rtems_libio_check_is_open( iop );
-    rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE, EINVAL );
+    LIBIO_GET_IOP_WITH_ACCESS( fd, iop, LIBIO_FLAGS_WRITE, EINVAL );
 
     rv = (*iop->pathinfo.handlers->ftruncate_h)( iop, length );
   } else {
diff --git a/cpukit/libcsupport/src/ioctl.c b/cpukit/libcsupport/src/ioctl.c
index e121cd0745..9fa7fa15a2 100644
--- a/cpukit/libcsupport/src/ioctl.c
+++ b/cpukit/libcsupport/src/ioctl.c
@@ -39,9 +39,7 @@ int ioctl(
   rtems_libio_t     *iop;
   void              *buffer;
 
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open(iop);
+  LIBIO_GET_IOP( fd, iop );
 
   va_start(ap, command);
 
diff --git a/cpukit/libcsupport/src/lseek.c b/cpukit/libcsupport/src/lseek.c
index f5c6fa6226..16271eba09 100644
--- a/cpukit/libcsupport/src/lseek.c
+++ b/cpukit/libcsupport/src/lseek.c
@@ -21,9 +21,7 @@ off_t lseek( int fd, off_t offset, int whence )
 {
   rtems_libio_t *iop;
 
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open(iop);
+  LIBIO_GET_IOP( fd, iop );
 
   return (*iop->pathinfo.handlers->lseek_h)( iop, offset, whence );
 }
diff --git a/cpukit/libcsupport/src/read.c b/cpukit/libcsupport/src/read.c
index 60c50eb875..d55ff180ae 100644
--- a/cpukit/libcsupport/src/read.c
+++ b/cpukit/libcsupport/src/read.c
@@ -35,10 +35,7 @@ ssize_t read(
   rtems_libio_check_buffer( buffer );
   rtems_libio_check_count( count );
 
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open( iop );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_READ, EBADF );
+  LIBIO_GET_IOP_WITH_ACCESS( fd, iop, LIBIO_FLAGS_READ, EBADF );
 
   /*
    *  Now process the read().
diff --git a/cpukit/libcsupport/src/write.c b/cpukit/libcsupport/src/write.c
index e98fb18a26..f44962afd8 100644
--- a/cpukit/libcsupport/src/write.c
+++ b/cpukit/libcsupport/src/write.c
@@ -38,10 +38,7 @@ ssize_t write(
   rtems_libio_check_buffer( buffer );
   rtems_libio_check_count( count );
 
-  rtems_libio_check_fd( fd );
-  iop = rtems_libio_iop( fd );
-  rtems_libio_check_is_open( iop );
-  rtems_libio_check_permissions( iop, LIBIO_FLAGS_WRITE, EBADF );
+  LIBIO_GET_IOP_WITH_ACCESS( fd, iop, LIBIO_FLAGS_WRITE, EBADF );
 
   /*
    *  Now process the write() request.
diff --git a/cpukit/libnetworking/rtems/rtems_syscall.c b/cpukit/libnetworking/rtems/rtems_syscall.c
index 37b330998a..21bf86a3a1 100644
--- a/cpukit/libnetworking/rtems/rtems_syscall.c
+++ b/cpukit/libnetworking/rtems/rtems_syscall.c
@@ -44,14 +44,13 @@ rtems_bsdnet_fdToSocket (int fd)
 {
   rtems_libio_t *iop;
 
-  /* same as rtems_libio_check_fd(_fd) but different return */
   if ((uint32_t)fd >= rtems_libio_number_iops) {
     errno = EBADF;
     return NULL;
   }
+
   iop = rtems_libio_iop(fd);
 
-  /* same as rtems_libio_check_is_open(iop) but different return */
   if ((rtems_libio_iop_flags(iop) & LIBIO_FLAGS_OPEN) == 0) {
     errno = EBADF;
     return NULL;
-- 
2.12.3




More information about the devel mailing list