[PATCH rtems-libbsd/6-freebsd-12 2/3] syscall/open: Reference the path info directory vnode

chrisj at rtems.org chrisj at rtems.org
Thu Jul 20 05:45:34 UTC 2023


From: Chris Johns <chrisj at rtems.org>

The kernel open call requires a path so to open a file we need to
set the current directory to the parent vnode. If the open mode is
create the path info vnode is the directory to perform the open
create in. Using the parent node creates the file in wrong path.

Updates #4723
---
 rtemsbsd/rtems/rtems-bsd-syscall-api.c | 24 ++++++---
 testsuite/nfs01/test_main.c            | 75 ++++++++++++++++----------
 2 files changed, 64 insertions(+), 35 deletions(-)

diff --git a/rtemsbsd/rtems/rtems-bsd-syscall-api.c b/rtemsbsd/rtems/rtems-bsd-syscall-api.c
index 76fc8ad7..cfceb768 100644
--- a/rtemsbsd/rtems/rtems-bsd-syscall-api.c
+++ b/rtemsbsd/rtems/rtems-bsd-syscall-api.c
@@ -931,9 +931,18 @@ rtems_bsd_sysgen_open_node(
 	} else {
 		rtems_filesystem_location_info_t *rootloc =
 		    &iop->pathinfo.mt_entry->mt_fs_root->location;
+		/*
+		 * We need the parent directory so open can find the
+		 * the entry. If we are creating the file the pathinfo
+		 * vnode entry is the directory to create open the
+		 * file in.
+		 */
 		cdir = rtems_bsd_libio_loc_to_vnode_dir(&iop->pathinfo);
+		if (cdir == NULL || creat) {
+			cdir = rtems_bsd_libio_loc_to_vnode(&iop->pathinfo);
+		}
 		if (fdp->fd_cdir == NULL) {
-			cdir = rtems_bsd_libio_loc_to_vnode(rootloc);
+			cdir = rtems_bsd_libio_loc_to_vnode_dir(rootloc);
 		} else if (rtems_bsd_libio_loc_to_vnode(&iop->pathinfo) ==
 				rtems_bsd_libio_loc_to_vnode(rootloc)) {
 			/*
@@ -958,11 +967,14 @@ rtems_bsd_sysgen_open_node(
 	FILEDESC_XUNLOCK(fdp);
 
 	if (RTEMS_BSD_SYSCALL_TRACE) {
-		printf("bsd: sys: open: path=%s opath=%s vn=%p cwd=%p"
-		       " flags=%08x mode=%08x isdir=%s\n",
-		    path, opath,
-		    creat ? NULL : rtems_bsd_libio_loc_to_vnode(&iop->pathinfo),
-		    fdp->fd_cdir, oflag, mode, isdir ? "yes" : "no");
+		struct vnode* _vn = rtems_bsd_libio_loc_to_vnode(&iop->pathinfo);
+		struct vnode* _dvn = rtems_bsd_libio_loc_to_vnode_dir(&iop->pathinfo);
+		printf("bsd: sys: open: path=%s opath=%s vn=%p (%c) dvn=%p (%c) cwd=%p"
+		       " flags=%08x mode=%o isdir=%s\n",
+		       path, opath,
+		       _vn, creat ? 'c' : _vn ? (_vn->v_type == VDIR ? 'd' : 'r') : 'n',
+		       _dvn,  _dvn ? (_dvn->v_type == VDIR ? 'd' : 'r') : 'n',
+		       fdp->fd_cdir, oflag, mode, isdir ? "yes" : "no");
 	}
 
 	VREF(fdp->fd_cdir);
diff --git a/testsuite/nfs01/test_main.c b/testsuite/nfs01/test_main.c
index cfa163e1..4ef82b44 100644
--- a/testsuite/nfs01/test_main.c
+++ b/testsuite/nfs01/test_main.c
@@ -46,7 +46,6 @@
 
 #include <rtems/console.h>
 #include <rtems/shell.h>
-#include <rtems/telnetd.h>
 
 #include <librtemsNfs.h>
 
@@ -58,6 +57,14 @@
 
 static const char *test_top = "test-nfs01";
 
+#define rtems_test_assert(__exp) \
+  do { \
+    if (!(__exp)) { \
+      printf( "%s: %d %s\n", __FILE__, __LINE__, #__exp ); \
+      assert(1 == 0); \
+    } \
+  } while (0)
+
 #define rtems_test_errno_assert(__exp) \
   do { \
     if (!(__exp)) { \
@@ -328,10 +335,46 @@ test_path_eval(const char *base, int depth)
 	test_cleanup(base);
 }
 
+static void
+test_path_file_copy(const char *base, int depth)
+{
+	char path[MAXPATHLEN];
+	struct stat sb;
+	FILE* f;
+	int l;
+
+	printf("test path eval\n");
+
+	test_setup(base);
+
+	memset(path, 0, sizeof(path));
+
+	for (l = 1; l <= depth; ++l) {
+		char* p = path + strlen(path);
+		if (l > 1) {
+			*p++ = '/';
+		}
+		snprintf(p, sizeof(path), "%d", l);
+		printf("test: nfs: mkdir: %s\n", path);
+		rtems_test_errno_assert(mkdir(path, 0777) == 0);
+	}
+
+	strlcat(path, "/test-file.txt", sizeof(path));
+	printf("Create file %s\n", path);
+	rtems_test_errno_assert((f = fopen(path, "w")) != NULL);
+	rtems_test_errno_assert(fprintf(f, "The contents of %s\nNFS test\n", path) > 0);
+	rtems_test_errno_assert(fclose(f) == 0);
+	printf("Checking %s has been copied\n", path);
+	rtems_test_errno_assert(stat(path, &sb) == 0);
+
+	test_cleanup(base);
+}
+
 static void
 test_nfs(const char *base)
 {
 	test_path_eval(base, 5);
+	test_path_file_copy(base, 3);
 #if NFS_TREE_WALK
 	test_printer_data pd;
 	memset(&pd, 0, sizeof(pd));
@@ -339,30 +382,6 @@ test_nfs(const char *base)
 #endif
 }
 
-static void
-telnet_shell(char *name, void *arg)
-{
-	rtems_shell_env_t env;
-
-	rtems_shell_dup_current_env(&env);
-
-	env.devname = name;
-	env.taskname = "TLNT";
-	env.login_check = NULL;
-	env.forever = false;
-
-	rtems_shell_main_loop(&env);
-}
-
-rtems_telnetd_config_table rtems_telnetd_config = {
-	.command = telnet_shell,
-	.arg = NULL,
-	.priority = 0,
-	.stack_size = 0,
-	.login_check = NULL,
-	.keep_stdio = false
-};
-
 static void
 test_main(void)
 {
@@ -373,8 +392,6 @@ test_main(void)
 	int retries = 0;
 	int rv;
 
-	assert(rtems_telnetd_initialize() == RTEMS_SUCCESSFUL);
-
 	if (strlen(options) != 0) {
 		mount_options = options;
 	}
@@ -399,11 +416,11 @@ test_main(void)
 
 	test_nfs(mount_point);
 
-	rtems_task_exit();
+	exit(0);
 }
 
 #define CONFIGURE_SHELL_COMMANDS_ALL
-#define DEFAULT_NETWORK_SHELL
+/* #define DEFAULT_NETWORK_SHELL */
 
 #define CONFIGURE_FILESYSTEM_NFS
 
-- 
2.37.1



More information about the devel mailing list