[PATCH 2/2] Support O_NOFOLLOW open() flag

Sebastian Huber sebastian.huber at embedded-brains.de
Thu Oct 18 10:42:59 UTC 2018


Close #3546.
---
 cpukit/libcsupport/src/open.c        |  7 +++-
 testsuites/psxtests/psxfile01/test.c | 67 ++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/cpukit/libcsupport/src/open.c b/cpukit/libcsupport/src/open.c
index 554311c5aa..86d0bbeab9 100644
--- a/cpukit/libcsupport/src/open.c
+++ b/cpukit/libcsupport/src/open.c
@@ -74,7 +74,12 @@ static int do_open(
   bool exclusive = (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL);
   bool truncate = (oflag & O_TRUNC) == O_TRUNC;
   bool open_dir;
-  int eval_flags = RTEMS_FS_FOLLOW_LINK
+#ifdef O_NOFOLLOW
+  int follow = (oflag & O_NOFOLLOW) == O_NOFOLLOW ? 0 : RTEMS_FS_FOLLOW_LINK;
+#else
+  int follow = RTEMS_FS_FOLLOW_LINK;
+#endif
+  int eval_flags = follow
     | (read_access ? RTEMS_FS_PERMS_READ : 0)
     | (write_access ? RTEMS_FS_PERMS_WRITE : 0)
     | (make ? RTEMS_FS_MAKE : 0)
diff --git a/testsuites/psxtests/psxfile01/test.c b/testsuites/psxtests/psxfile01/test.c
index 129412d3ef..1ea3dad503 100644
--- a/testsuites/psxtests/psxfile01/test.c
+++ b/testsuites/psxtests/psxfile01/test.c
@@ -59,6 +59,8 @@ rtems_filesystem_operations_table  IMFS_ops_no_rename;
 
 static const char somefile[] = "somefile";
 
+static const char somelink[] = "somelink";
+
 /*
  *  File test support routines.
  */
@@ -152,6 +154,11 @@ static void test_open_directory(void)
 
   status = unlink( somefile );
   rtems_test_assert( status == 0 );
+
+  errno = 0;
+  fd = open( somefile, O_RDONLY );
+  rtems_test_assert( fd == -1 );
+  rtems_test_assert( errno == ENOENT );
 }
 
 static void test_open_cloexec(void)
@@ -174,6 +181,65 @@ static void test_open_cloexec(void)
 
   status = unlink( somefile );
   rtems_test_assert( status == 0 );
+
+  errno = 0;
+  fd = open( somefile, O_RDONLY );
+  rtems_test_assert( fd == -1 );
+  rtems_test_assert( errno == ENOENT );
+}
+
+static void test_open_nofollow(void)
+{
+  int status;
+  int fd;
+  struct stat st;
+
+  fd = open( somefile, O_CREAT, S_IRWXU );
+  rtems_test_assert( fd >= 0 );
+
+  status = close( fd );
+  rtems_test_assert( status == 0 );
+
+  status = symlink( somefile, somelink );
+  rtems_test_assert( status == 0 );
+
+  fd = open( somelink, O_RDONLY );
+  rtems_test_assert( fd >= 0 );
+
+  status = fstat( fd, &st );
+  rtems_test_assert( status == 0 );
+  rtems_test_assert( S_ISREG( st.st_mode ) );
+
+  status = close( fd );
+  rtems_test_assert( status == 0 );
+
+#ifdef O_NOFOLLOW
+  fd = open( somelink, O_RDONLY | O_NOFOLLOW );
+  rtems_test_assert( fd >= 0 );
+
+  status = fstat( fd, &st );
+  rtems_test_assert( status == 0 );
+  rtems_test_assert( S_ISLNK( st.st_mode ) );
+
+  status = close( fd );
+  rtems_test_assert( status == 0 );
+#endif
+
+  status = unlink( somelink );
+  rtems_test_assert( status == 0 );
+
+  errno = 0;
+  fd = open( somelink, O_RDONLY );
+  rtems_test_assert( fd == -1 );
+  rtems_test_assert( errno == ENOENT );
+
+  status = unlink( somefile );
+  rtems_test_assert( status == 0 );
+
+  errno = 0;
+  fd = open( somefile, O_RDONLY );
+  rtems_test_assert( fd == -1 );
+  rtems_test_assert( errno == ENOENT );
 }
 
 /*
@@ -209,6 +275,7 @@ int main(
 
   test_open_directory();
   test_open_cloexec();
+  test_open_nofollow();
 
   /*
    *  Grab the maximum size of an in-memory file.
-- 
2.16.4



More information about the devel mailing list