[PATCH 2/2] Filesystem: Change rtems_filesystem_chown()

Sebastian Huber sebastian.huber at embedded-brains.de
Wed Sep 11 09:01:12 UTC 2013


Implement POSIX requirements in the high-level file system layer.  Use
common implementation for all change owner variants.
---
 cpukit/libcsupport/include/rtems/libio_.h |    5 +--
 cpukit/libcsupport/src/chown.c            |   25 ++++----------
 cpukit/libcsupport/src/fchown.c           |   52 +++++++++++++++++++++-------
 cpukit/libcsupport/src/lchown.c           |   17 ++++++---
 4 files changed, 59 insertions(+), 40 deletions(-)

diff --git a/cpukit/libcsupport/include/rtems/libio_.h b/cpukit/libcsupport/include/rtems/libio_.h
index 4a0623e..8856566 100644
--- a/cpukit/libcsupport/include/rtems/libio_.h
+++ b/cpukit/libcsupport/include/rtems/libio_.h
@@ -572,10 +572,9 @@ int rtems_filesystem_chmod(
 );
 
 int rtems_filesystem_chown(
-  const char *path,
+  const rtems_filesystem_location_info_t *loc,
   uid_t owner,
-  gid_t group,
-  int eval_follow_link
+  gid_t group
 );
 
 static inline bool rtems_filesystem_is_ready_for_unmount(
diff --git a/cpukit/libcsupport/src/chown.c b/cpukit/libcsupport/src/chown.c
index 434fc2b..08ad8a2 100644
--- a/cpukit/libcsupport/src/chown.c
+++ b/cpukit/libcsupport/src/chown.c
@@ -25,31 +25,20 @@
 
 #include <rtems/libio_.h>
 
-int rtems_filesystem_chown(
-  const char *path,
-  uid_t owner,
-  gid_t group,
-  int eval_follow_link
-)
+/**
+ *  POSIX 1003.1b 5.6.5 - Change Owner and Group of a File
+ */
+int chown( const char *path, uid_t owner, gid_t group )
 {
-  int rv = 0;
+  int rv;
   rtems_filesystem_eval_path_context_t ctx;
-  int eval_flags = eval_follow_link;
+  int eval_flags = RTEMS_FS_FOLLOW_LINK;
   const rtems_filesystem_location_info_t *currentloc =
     rtems_filesystem_eval_path_start( &ctx, path, eval_flags );
-  const rtems_filesystem_operations_table *ops = currentloc->mt_entry->ops;
 
-  rv = (*ops->chown_h)( currentloc, owner, group );
+  rv = rtems_filesystem_chown( currentloc, owner, group );
 
   rtems_filesystem_eval_path_cleanup( &ctx );
 
   return rv;
 }
-
-/**
- *  POSIX 1003.1b 5.6.5 - Change Owner and Group of a File
- */
-int chown( const char *path, uid_t owner, gid_t group )
-{
-  return rtems_filesystem_chown( path, owner, group, RTEMS_FS_FOLLOW_LINK );
-}
diff --git a/cpukit/libcsupport/src/fchown.c b/cpukit/libcsupport/src/fchown.c
index ec84fe4..2e74159 100644
--- a/cpukit/libcsupport/src/fchown.c
+++ b/cpukit/libcsupport/src/fchown.c
@@ -22,30 +22,56 @@
 
 #include <rtems/libio_.h>
 
+int rtems_filesystem_chown(
+  const rtems_filesystem_location_info_t *loc,
+  uid_t owner,
+  gid_t group
+)
+{
+  const rtems_filesystem_mount_table_entry_t *mt_entry = loc->mt_entry;
+  int rv;
+
+  if ( mt_entry->writeable || rtems_filesystem_location_is_null( loc ) ) {
+    struct stat st;
+
+    memset( &st, 0, sizeof(st) );
+
+    rv = (*loc->handlers->fstat_h)( loc, &st );
+    if ( rv == 0 ) {
+      uid_t uid = geteuid();
+
+      if ( uid == 0 || st.st_uid == uid ) {
+        rv = (*mt_entry->ops->chown_h)( loc, owner, group );
+      } else {
+        errno = EPERM;
+        rv = -1;
+      }
+    }
+  } else {
+    errno = EROFS;
+    rv = -1;
+  }
+
+  return rv;
+}
+
 /**
  *  POSIX 1003.1b 5.6.5 - Change Owner and Group of a File
  */
 int fchown( int fd, uid_t owner, gid_t group )
 {
-  int rv = 0;
+  int rv;
   rtems_libio_t *iop;
 
   rtems_libio_check_fd( fd );
   iop = rtems_libio_iop( fd );
   rtems_libio_check_is_open(iop);
 
-  if (iop->pathinfo.mt_entry->writeable) {
-    rtems_filesystem_instance_lock( &iop->pathinfo );
-    rv = (*iop->pathinfo.mt_entry->ops->chown_h)(
-      &iop->pathinfo,
-      owner,
-      group
-    );
-    rtems_filesystem_instance_unlock( &iop->pathinfo );
-  } else {
-    errno = EROFS;
-    rv = -1;
-  }
+  rtems_filesystem_instance_lock( &iop->pathinfo );
+
+  rv = rtems_filesystem_chown( &iop->pathinfo, owner, group );
+
+  rtems_filesystem_instance_unlock( &iop->pathinfo );
 
   return rv;
 }
diff --git a/cpukit/libcsupport/src/lchown.c b/cpukit/libcsupport/src/lchown.c
index 406ac59..d6d20d6 100644
--- a/cpukit/libcsupport/src/lchown.c
+++ b/cpukit/libcsupport/src/lchown.c
@@ -19,10 +19,15 @@
 
 int lchown( const char *path, uid_t owner, gid_t group )
 {
-  return rtems_filesystem_chown(
-    path,
-    owner,
-    group,
-    RTEMS_FS_FOLLOW_HARD_LINK
-  );
+  int rv;
+  rtems_filesystem_eval_path_context_t ctx;
+  int eval_flags = RTEMS_FS_FOLLOW_HARD_LINK;
+  const rtems_filesystem_location_info_t *currentloc =
+    rtems_filesystem_eval_path_start( &ctx, path, eval_flags );
+
+  rv = rtems_filesystem_chown( currentloc, owner, group );
+
+  rtems_filesystem_eval_path_cleanup( &ctx );
+
+  return rv;
 }
-- 
1.7.7




More information about the devel mailing list