casaroli commented on code in PR #21907:
URL: https://github.com/apache/nuttx/pull/21907#discussion_r4711479534


##########
fs/vfs/fs_cwd.c:
##########
@@ -0,0 +1,625 @@
+/****************************************************************************
+ * fs/vfs/fs_cwd.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/lib/lib.h>
+#include <nuttx/mutex.h>
+#include <nuttx/sched.h>
+
+#include "inode/inode.h"
+#include "fs_heap.h"
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* tg_cwd of each task group is the canonical absolute host path of its
+ * current directory, in the kernel heap.  NULL is the home directory and
+ * "" is a removed directory.  g_cwd_lock protects tg_cwd of all groups.
+ */
+
+static mutex_t g_cwd_lock = NXMUTEX_INITIALIZER;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: cwd_root
+ *
+ * Description:
+ *   Return the chroot() root of the calling task group, or NULL.
+ *
+ ****************************************************************************/
+
+static FAR const char *cwd_root(void)
+{
+#ifdef CONFIG_FS_CHROOT
+  FAR struct tcb_s *tcb = nxsched_self();
+
+  if (tcb != NULL && tcb->group != NULL)
+    {
+      return tcb->group->tg_root;
+    }
+#endif
+
+  return NULL;
+}
+
+/****************************************************************************
+ * Name: cwd_userpath
+ *
+ * Description:
+ *   Return the part of a host path that the caller uses: the path relative
+ *   to the root of the caller.  NULL if the path is outside the root.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_CHROOT
+static FAR const char *cwd_userpath(FAR const char *path)
+{
+  FAR const char *root = cwd_root();
+  size_t len;
+
+  if (root == NULL)
+    {
+      return path;
+    }
+
+  len = strlen(root);
+  if (strncmp(path, root, len) != 0 ||
+      (path[len] != '/' && path[len] != '\0'))
+    {
+      return NULL;
+    }
+
+  return path[len] == '\0' ? "/" : path + len;
+}
+#else
+#  define cwd_userpath(path) (path)
+#endif
+
+/****************************************************************************
+ * Name: cwd_under
+ *
+ * Description:
+ *   Return true if 'path' is 'dir' or a path below 'dir'.
+ *
+ ****************************************************************************/
+
+static bool cwd_under(FAR const char *path, FAR const char *dir)
+{
+  size_t len = strlen(dir);
+
+  if (len == 1 && dir[0] == '/')
+    {
+      return true;
+    }
+
+  return strncmp(path, dir, len) == 0 &&
+         (path[len] == '\0' || path[len] == '/');
+}
+
+/****************************************************************************
+ * Name: cwd_searchable
+ *
+ * Description:
+ *   Return true if the effective IDs of the caller have search permission
+ *   on the directory.  Root needs any execute bit, as in access() and
+ *   exec().
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_PERMISSION
+static bool cwd_searchable(FAR const struct stat *buf)
+{
+  mode_t xbit = S_IXOTH;
+  uid_t uid = geteuid();
+
+  if (uid == 0)
+    {
+      return (buf->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
+    }
+
+  if (buf->st_uid == uid)
+    {
+      xbit = S_IXUSR;
+    }
+  else if (buf->st_gid == getegid())
+    {
+      xbit = S_IXGRP;
+    }
+#if NGROUPS_MAX > 0
+  else
+    {
+      gid_t groups[NGROUPS_MAX];
+      int ngroups = getgroups(NGROUPS_MAX, groups);
+      int i;
+
+      for (i = 0; i < ngroups; i++)
+        {
+          if (groups[i] == buf->st_gid)
+            {
+              xbit = S_IXGRP;
+              break;
+            }
+        }
+    }
+#endif
+
+  return (buf->st_mode & xbit) != 0;
+}
+#endif
+
+/****************************************************************************
+ * Name: cwd_tohost
+ *
+ * Description:
+ *   Put the root of the caller in front of an absolute path in 'buf', in
+ *   place.  'buf' holds PATH_MAX bytes.
+ *
+ ****************************************************************************/
+
+static int cwd_tohost(FAR char *buf)
+{
+  FAR const char *root = cwd_root();
+  size_t rootlen;
+  size_t len;
+
+  if (root == NULL)
+    {
+      return OK;
+    }
+
+  rootlen = strlen(root);
+  len     = strlen(buf);
+  if (len == 1)
+    {
+      /* The root itself: no "/" after it */
+
+      buf[0] = '\0';
+      len    = 0;
+    }
+
+  if (rootlen + len >= PATH_MAX)
+    {
+      return -ENAMETOOLONG;
+    }
+
+  memmove(buf + rootlen, buf, len + 1);
+  memcpy(buf, root, rootlen);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: cwd_set
+ *
+ * Description:
+ *   Make 'path' the current directory of the task group of the caller.
+ *   'path' is an absolute path inside the root of the caller.  A buffer
+ *   'buf' of PATH_MAX bytes is given for the work.
+ *
+ ****************************************************************************/
+
+static int cwd_set(FAR const char *path, FAR char *buf)
+{
+  FAR struct task_group_s *group = nxsched_self()->group;
+  FAR char *cwd;
+  FAR char *old;
+  struct stat sb;
+  int ret;
+
+  /* The canonical path: absolute, no ".", ".." or symbolic links.  The
+   * search permission of each component is checked on the way.
+   */
+
+  if (lib_realpath(path, buf, false) == NULL)
+    {
+      return -get_errno();
+    }
+
+  ret = nx_stat(buf, &sb, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (!S_ISDIR(sb.st_mode))
+    {
+      return -ENOTDIR;
+    }
+
+#ifdef CONFIG_FS_PERMISSION
+  /* stat() needs no search permission on the last component */
+
+  if (!cwd_searchable(&sb))
+    {
+      return -EACCES;
+    }
+#endif
+
+  ret = cwd_tohost(buf);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  cwd = fs_heap_strdup(buf);
+  if (cwd == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  nxmutex_lock(&g_cwd_lock);
+  old = group->tg_cwd;
+  group->tg_cwd = cwd;
+  nxmutex_unlock(&g_cwd_lock);
+
+  fs_heap_free(old);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: cwd_get
+ *
+ * Description:
+ *   Copy the current directory of the caller to 'buf' of 'size' bytes as
+ *   the path that the caller uses: inside its root.  Return the length
+ *   with the NUL.
+ *
+ ****************************************************************************/
+
+static int cwd_get(FAR char *buf, size_t size)
+{
+  FAR const char *cwd;
+  FAR const char *path = CONFIG_LIBC_HOMEDIR;
+  size_t len;
+  int ret;
+
+  nxmutex_lock(&g_cwd_lock);
+  cwd = nxsched_self()->group->tg_cwd;
+  if (cwd != NULL)
+    {
+      /* A directory outside the root has no name for the caller.  The
+       * default, the home directory, is inside the root.
+       */
+
+      path = cwd[0] == '\0' ? NULL : cwd_userpath(cwd);
+    }
+
+  if (path == NULL)
+    {
+      ret = -ENOENT;
+    }
+  else
+    {
+      len = strlen(path) + 1;
+      if (len > size)
+        {
+          ret = -ERANGE;
+        }
+      else
+        {
+          memcpy(buf, path, len);
+          ret = len;
+        }
+    }
+
+  nxmutex_unlock(&g_cwd_lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fs_cwd_inherit
+ *
+ * Description:
+ *   See include/nuttx/fs/fs.h.
+ *
+ ****************************************************************************/
+
+int fs_cwd_inherit(FAR struct task_group_s *group)
+{
+  FAR struct tcb_s *rtcb = nxsched_self();
+  FAR const char *parent;
+  int ret = OK;
+
+  if (rtcb == NULL || rtcb->group == NULL || rtcb->group == group)
+    {
+      return OK;
+    }
+
+  nxmutex_lock(&g_cwd_lock);
+  parent = rtcb->group->tg_cwd;
+  if (parent != NULL)
+    {
+      group->tg_cwd = fs_heap_strdup(parent);
+      if (group->tg_cwd == NULL)
+        {
+          ret = -ENOMEM;
+        }
+    }
+
+  nxmutex_unlock(&g_cwd_lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: fs_cwd_release
+ *
+ * Description:
+ *   See include/nuttx/fs/fs.h.
+ *
+ ****************************************************************************/
+
+void fs_cwd_release(FAR struct task_group_s *group)
+{
+  FAR char *cwd;
+
+  nxmutex_lock(&g_cwd_lock);
+  cwd = group->tg_cwd;
+  group->tg_cwd = NULL;
+  nxmutex_unlock(&g_cwd_lock);
+
+  fs_heap_free(cwd);
+}
+
+/****************************************************************************
+ * Name: fs_cwd_join
+ *
+ * Description:
+ *   See fs/inode/inode.h.
+ *
+ ****************************************************************************/
+
+int fs_cwd_join(FAR const char *path, FAR char *buffer, size_t buflen)
+{
+  FAR const char *cwd;
+  FAR const char *root = cwd_root();
+  int ret = OK;
+  int len = 0;
+
+  nxmutex_lock(&g_cwd_lock);
+  cwd = nxsched_self()->group->tg_cwd;
+  if (cwd == NULL)
+    {
+      /* The default: the home directory, inside the root of the caller */
+
+      len = snprintf(buffer, buflen, "%s%s/%s", root != NULL ? root : "",
+                     CONFIG_LIBC_HOMEDIR, path);
+    }
+  else if (cwd[0] == '\0')
+    {
+      ret = -ENOENT;
+    }
+  else if (root != NULL && !cwd_under(cwd, root))
+    {
+      /* A directory outside the root (chroot() without chdir()) is not the
+       * start of a lookup: relative names cannot leave the root.
+       */
+
+      ret = -ENOENT;
+    }
+  else
+    {
+      len = snprintf(buffer, buflen, "%s/%s", cwd, path);
+    }
+
+  nxmutex_unlock(&g_cwd_lock);
+
+  /* The size that the joined path needs, with its NUL */
+
+  return ret < 0 ? ret : len + 1;
+}
+
+/****************************************************************************
+ * Name: chdir
+ *
+ * Description:
+ *   The chdir() function causes the directory named by the pathname pointed
+ *   to by the 'path' argument to become the current working directory; that
+ *   is, the starting point for path searches for pathnames not beginning
+ *   with '/'.  The current directory is an attribute of the task group.
+ *   The kernel threads share one: chdir() in one changes it for all.
+ *   chdir() does not change the environment variable PWD.
+ *
+ * Input Parameters:
+ *   path - A pointer to a directory to use as the new current working
+ *     directory
+ *
+ * Returned Value:
+ *   0(OK) on success; -1(ERROR) on failure with errno set appropriately:
+ *
+ *   EACCES
+ *     Search permission is denied for any component of the pathname.
+ *   ELOOP
+ *     A loop exists in symbolic links encountered during resolution of the
+ *     'path' argument.
+ *   ENAMETOOLONG
+ *     The length of the path argument exceeds PATH_MAX or a pathname
+ *     component is longer than NAME_MAX.
+ *   ENOENT
+ *     A component of 'path' does not name an existing directory or path is
+ *     an empty string.
+ *   ENOTDIR
+ *     A component of the pathname is not a directory.
+ *
+ ****************************************************************************/
+
+int chdir(FAR const char *path)
+{
+  FAR char *buf;
+  int ret;
+
+  if (path == NULL)
+    {
+      ret = -EFAULT;
+      goto errout;
+    }
+
+  buf = lib_get_tempbuffer(PATH_MAX);
+  if (buf == NULL)
+    {
+      ret = -ENOMEM;
+      goto errout;
+    }
+
+  ret = cwd_set(path, buf);
+  lib_put_tempbuffer(buf);
+  if (ret >= 0)
+    {
+      return OK;
+    }
+
+errout:
+  set_errno(-ret);
+  return ERROR;
+}
+
+/****************************************************************************
+ * Name: fchdir
+ *
+ * Description:
+ *   The fchdir() function is the same as chdir(), with the directory given
+ *   as an open file descriptor.  The directory is found by the path that it
+ *   had when it was opened.
+ *
+ * Input Parameters:
+ *   fd - An open file descriptor of a directory.
+ *
+ * Returned Value:
+ *   0(OK) on success; -1(ERROR) on failure with errno set appropriately:
+ *
+ *   EACCES
+ *     Search permission was denied on the directory open on fd.
+ *   EBADF
+ *     fd is not a valid file descriptor.
+ *   ENOTDIR
+ *     fd does not refer to a directory.
+ *   ENOSYS
+ *     CONFIG_FS_IOCTL_FILEPATH is not enabled.
+ *
+ ****************************************************************************/
+
+int fchdir(int fd)
+{
+  FAR struct file *filep;
+  FAR const char *user;
+  FAR char *path;
+  FAR char *buf;
+  int ret;
+
+  ret = file_get(fd, &filep);
+  if (ret < 0)
+    {
+      goto errout;
+    }
+
+  path = lib_get_tempbuffer(PATH_MAX);
+  buf  = lib_get_tempbuffer(PATH_MAX);
+  if (path == NULL || buf == NULL)
+    {
+      ret = -ENOMEM;
+    }
+  else
+    {
+      /* A directory always has a path.  A file without one (ENOTTY, or a
+       * name that is not a path, as for a socket) is not a directory.
+       * cwd_set() gives ENOTDIR for a file that has a path.
+       */
+
+      ret = file_fcntl(filep, F_GETPATH, path);
+      if (ret == -ENOTTY || (ret >= 0 && path[0] != '/'))
+        {
+#ifdef CONFIG_FS_IOCTL_FILEPATH
+          ret = -ENOTDIR;
+#else
+          ret = -ENOSYS;  /* No file has a path */
+#endif
+        }
+    }
+
+  file_put(filep);
+
+  if (ret >= 0)
+    {
+      /* The path that the caller uses, inside its root */
+
+      user = cwd_userpath(path);
+      ret  = user != NULL ? cwd_set(user, buf) : -ENOENT;
+    }
+
+  if (buf != NULL)
+    {
+      lib_put_tempbuffer(buf);
+    }
+
+  if (path != NULL)
+    {
+      lib_put_tempbuffer(path);
+    }
+
+  if (ret >= 0)
+    {
+      return OK;
+    }
+
+errout:
+  set_errno(-ret);
+  return ERROR;
+}
+
+/****************************************************************************
+ * Name: nx_getcwd
+ *
+ * Description:
+ *   See include/nuttx/fs/fs.h.
+ *
+ ****************************************************************************/
+
+int nx_getcwd(FAR char *buf, size_t size)

Review Comment:
   Done.
   



##########
fs/vfs/fs_cwd.c:
##########
@@ -0,0 +1,625 @@
+/****************************************************************************
+ * fs/vfs/fs_cwd.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/lib/lib.h>
+#include <nuttx/mutex.h>
+#include <nuttx/sched.h>
+
+#include "inode/inode.h"
+#include "fs_heap.h"
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* tg_cwd of each task group is the canonical absolute host path of its
+ * current directory, in the kernel heap.  NULL is the home directory and
+ * "" is a removed directory.  g_cwd_lock protects tg_cwd of all groups.

Review Comment:
   `rename()` gives the new path to a task group that the walk returns, after 
`nxsched_foreach()` ends (fs_cwd.c:645-657), so that group must stay alive 
until then. `fs_cwd_release()` takes the same lock for this; a lock inside the 
group cannot keep the group alive.
   



##########
fs/vfs/fs_cwd.c:
##########
@@ -0,0 +1,625 @@
+/****************************************************************************
+ * fs/vfs/fs_cwd.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/stat.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <nuttx/fs/fs.h>
+#include <nuttx/lib/lib.h>
+#include <nuttx/mutex.h>
+#include <nuttx/sched.h>
+
+#include "inode/inode.h"
+#include "fs_heap.h"
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* tg_cwd of each task group is the canonical absolute host path of its
+ * current directory, in the kernel heap.  NULL is the home directory and
+ * "" is a removed directory.  g_cwd_lock protects tg_cwd of all groups.
+ */
+
+static mutex_t g_cwd_lock = NXMUTEX_INITIALIZER;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: cwd_root
+ *
+ * Description:
+ *   Return the chroot() root of the calling task group, or NULL.
+ *
+ ****************************************************************************/
+
+static FAR const char *cwd_root(void)
+{
+#ifdef CONFIG_FS_CHROOT
+  FAR struct tcb_s *tcb = nxsched_self();
+
+  if (tcb != NULL && tcb->group != NULL)
+    {
+      return tcb->group->tg_root;
+    }
+#endif
+
+  return NULL;
+}
+
+/****************************************************************************
+ * Name: cwd_userpath
+ *
+ * Description:
+ *   Return the part of a host path that the caller uses: the path relative
+ *   to the root of the caller.  NULL if the path is outside the root.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_CHROOT
+static FAR const char *cwd_userpath(FAR const char *path)
+{
+  FAR const char *root = cwd_root();
+  size_t len;
+
+  if (root == NULL)
+    {
+      return path;
+    }
+
+  len = strlen(root);
+  if (strncmp(path, root, len) != 0 ||
+      (path[len] != '/' && path[len] != '\0'))
+    {
+      return NULL;
+    }
+
+  return path[len] == '\0' ? "/" : path + len;
+}
+#else
+#  define cwd_userpath(path) (path)
+#endif
+
+/****************************************************************************
+ * Name: cwd_under
+ *
+ * Description:
+ *   Return true if 'path' is 'dir' or a path below 'dir'.
+ *
+ ****************************************************************************/
+
+static bool cwd_under(FAR const char *path, FAR const char *dir)
+{
+  size_t len = strlen(dir);
+
+  if (len == 1 && dir[0] == '/')
+    {
+      return true;
+    }
+
+  return strncmp(path, dir, len) == 0 &&
+         (path[len] == '\0' || path[len] == '/');
+}
+
+/****************************************************************************
+ * Name: cwd_searchable
+ *
+ * Description:
+ *   Return true if the effective IDs of the caller have search permission
+ *   on the directory.  Root needs any execute bit, as in access() and
+ *   exec().
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_FS_PERMISSION
+static bool cwd_searchable(FAR const struct stat *buf)
+{
+  mode_t xbit = S_IXOTH;
+  uid_t uid = geteuid();
+
+  if (uid == 0)
+    {
+      return (buf->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0;
+    }
+
+  if (buf->st_uid == uid)
+    {
+      xbit = S_IXUSR;
+    }
+  else if (buf->st_gid == getegid())
+    {
+      xbit = S_IXGRP;
+    }
+#if NGROUPS_MAX > 0
+  else
+    {
+      gid_t groups[NGROUPS_MAX];
+      int ngroups = getgroups(NGROUPS_MAX, groups);
+      int i;
+
+      for (i = 0; i < ngroups; i++)
+        {
+          if (groups[i] == buf->st_gid)
+            {
+              xbit = S_IXGRP;
+              break;
+            }
+        }
+    }
+#endif
+
+  return (buf->st_mode & xbit) != 0;
+}
+#endif
+
+/****************************************************************************
+ * Name: cwd_tohost
+ *
+ * Description:
+ *   Put the root of the caller in front of an absolute path in 'buf', in
+ *   place.  'buf' holds PATH_MAX bytes.
+ *
+ ****************************************************************************/
+
+static int cwd_tohost(FAR char *buf)
+{
+  FAR const char *root = cwd_root();
+  size_t rootlen;
+  size_t len;
+
+  if (root == NULL)
+    {
+      return OK;
+    }
+
+  rootlen = strlen(root);
+  len     = strlen(buf);
+  if (len == 1)
+    {
+      /* The root itself: no "/" after it */
+
+      buf[0] = '\0';
+      len    = 0;
+    }
+
+  if (rootlen + len >= PATH_MAX)
+    {
+      return -ENAMETOOLONG;
+    }
+
+  memmove(buf + rootlen, buf, len + 1);
+  memcpy(buf, root, rootlen);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: cwd_set
+ *
+ * Description:
+ *   Make 'path' the current directory of the task group of the caller.
+ *   'path' is an absolute path inside the root of the caller.  A buffer
+ *   'buf' of PATH_MAX bytes is given for the work.
+ *
+ ****************************************************************************/
+
+static int cwd_set(FAR const char *path, FAR char *buf)
+{
+  FAR struct task_group_s *group = nxsched_self()->group;
+  FAR char *cwd;
+  FAR char *old;
+  struct stat sb;
+  int ret;
+
+  /* The canonical path: absolute, no ".", ".." or symbolic links.  The
+   * search permission of each component is checked on the way.
+   */
+
+  if (lib_realpath(path, buf, false) == NULL)
+    {
+      return -get_errno();
+    }
+
+  ret = nx_stat(buf, &sb, 1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  if (!S_ISDIR(sb.st_mode))
+    {
+      return -ENOTDIR;
+    }
+
+#ifdef CONFIG_FS_PERMISSION
+  /* stat() needs no search permission on the last component */
+
+  if (!cwd_searchable(&sb))
+    {
+      return -EACCES;
+    }
+#endif
+
+  ret = cwd_tohost(buf);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
+  cwd = fs_heap_strdup(buf);
+  if (cwd == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  nxmutex_lock(&g_cwd_lock);
+  old = group->tg_cwd;
+  group->tg_cwd = cwd;
+  nxmutex_unlock(&g_cwd_lock);
+
+  fs_heap_free(old);
+  return OK;
+}
+
+/****************************************************************************
+ * Name: cwd_get
+ *
+ * Description:
+ *   Copy the current directory of the caller to 'buf' of 'size' bytes as
+ *   the path that the caller uses: inside its root.  Return the length
+ *   with the NUL.
+ *
+ ****************************************************************************/
+
+static int cwd_get(FAR char *buf, size_t size)
+{
+  FAR const char *cwd;
+  FAR const char *path = CONFIG_LIBC_HOMEDIR;
+  size_t len;
+  int ret;
+
+  nxmutex_lock(&g_cwd_lock);
+  cwd = nxsched_self()->group->tg_cwd;
+  if (cwd != NULL)
+    {
+      /* A directory outside the root has no name for the caller.  The
+       * default, the home directory, is inside the root.
+       */
+
+      path = cwd[0] == '\0' ? NULL : cwd_userpath(cwd);
+    }
+
+  if (path == NULL)
+    {
+      ret = -ENOENT;
+    }
+  else
+    {
+      len = strlen(path) + 1;
+      if (len > size)
+        {
+          ret = -ERANGE;
+        }
+      else
+        {
+          memcpy(buf, path, len);
+          ret = len;
+        }
+    }
+
+  nxmutex_unlock(&g_cwd_lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: fs_cwd_inherit
+ *
+ * Description:
+ *   See include/nuttx/fs/fs.h.
+ *
+ ****************************************************************************/
+
+int fs_cwd_inherit(FAR struct task_group_s *group)
+{
+  FAR struct tcb_s *rtcb = nxsched_self();
+  FAR const char *parent;
+  int ret = OK;
+
+  if (rtcb == NULL || rtcb->group == NULL || rtcb->group == group)
+    {
+      return OK;
+    }
+
+  nxmutex_lock(&g_cwd_lock);
+  parent = rtcb->group->tg_cwd;
+  if (parent != NULL)
+    {
+      group->tg_cwd = fs_heap_strdup(parent);
+      if (group->tg_cwd == NULL)
+        {
+          ret = -ENOMEM;
+        }
+    }
+
+  nxmutex_unlock(&g_cwd_lock);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: fs_cwd_release
+ *
+ * Description:
+ *   See include/nuttx/fs/fs.h.
+ *
+ ****************************************************************************/
+
+void fs_cwd_release(FAR struct task_group_s *group)
+{
+  FAR char *cwd;
+
+  nxmutex_lock(&g_cwd_lock);

Review Comment:
   The walk of `rename()`, `rmdir()` and `umount()` in the next commit reads 
`tg_cwd` of all task groups. This lock makes `fs_cwd_release()` wait until a 
walk ends, so a walk never reads a task group that is freed (fs_cwd.c:715-722).
   



##########
fs/vfs/fs_cwd.c:
##########
@@ -343,6 +359,302 @@ static int cwd_get(FAR char *buf, size_t size)
   return ret;
 }
 
+/****************************************************************************
+ * Name: cwd_hostpath
+ *
+ * Description:
+ *   Write the canonical host path of 'path' to 'buf' (PATH_MAX bytes): the
+ *   form of the paths that chdir() keeps.  The last component is not
+ *   followed: only its directory is resolved, so this works after the
+ *   last component was removed or renamed.
+ *
+ ****************************************************************************/
+
+static int cwd_hostpath(FAR const char *path, FAR char *buf)
+{
+  FAR const char *base;
+  FAR char *dir = NULL;
+  size_t baselen = 0;
+  size_t len;
+  int ret;
+
+  len = strlen(path);
+  while (len > 1 && path[len - 1] == '/')
+    {
+      len--;
+    }
+
+  /* The last component, and its directory */
+
+  base = path + len;
+  while (base > path && *(base - 1) != '/')
+    {
+      base--;
+    }
+
+  baselen = path + len - base;
+  if (baselen == 0 || baselen > NAME_MAX ||
+      (base[0] == '.' && (baselen == 1 || (baselen == 2 && base[1] == '.'))))
+    {
+      base = NULL;     /* "/", "." or "..": resolve the whole path */
+    }
+
+  if (base != NULL)
+    {
+      if (base - path > PATH_MAX)
+        {
+          return -ENAMETOOLONG;
+        }
+
+      dir = lib_get_tempbuffer(PATH_MAX);
+      if (dir == NULL)
+        {
+          return -ENOMEM;
+        }
+
+      if (base == path)
+        {
+          strlcpy(dir, ".", PATH_MAX);
+        }
+      else if (base == path + 1)
+        {
+          strlcpy(dir, "/", PATH_MAX);
+        }
+      else
+        {
+          strlcpy(dir, path, base - path);
+        }
+
+      path = dir;
+    }
+
+  if (lib_realpath(path, buf, false) == NULL)
+    {
+      ret = -get_errno();
+    }
+  else
+    {
+      ret = OK;
+      if (base != NULL)
+        {
+          len = strlen(buf);
+          if (len > 1)
+            {
+              buf[len++] = '/';
+            }
+
+          if (len + baselen >= PATH_MAX)
+            {
+              ret = -ENAMETOOLONG;
+            }
+          else
+            {
+              memcpy(buf + len, base, baselen);
+              buf[len + baselen] = '\0';
+            }
+        }
+
+      if (ret >= 0)
+        {
+          ret = cwd_tohost(buf);
+        }
+    }
+
+  if (dir != NULL)
+    {
+      lib_put_tempbuffer(dir);
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: cwd_mounted
+ *
+ * Description:
+ *   Return true if the path 'rest' below the mount point 'node' goes
+ *   through another mount point in the inode tree below it.  The caller
+ *   holds the inode tree lock.
+ *
+ ****************************************************************************/
+
+static bool cwd_mounted(FAR struct inode *node, FAR const char *rest)
+{
+  FAR struct inode *child;
+  size_t len;
+
+  while (*rest == '/')
+    {
+      rest++;
+      len = strcspn(rest, "/");
+      for (child = node->i_child; child != NULL; child = child->i_peer)
+        {
+          if (strncmp(child->i_name, rest, len) == 0 &&
+              child->i_name[len] == '\0')
+            {
+              break;
+            }
+        }
+
+      if (child == NULL)
+        {
+          return false;
+        }
+
+      if (INODE_IS_MOUNTPT(child))
+        {
+          return true;
+        }
+
+      node  = child;
+      rest += len;
+    }
+
+  return false;
+}
+
+/****************************************************************************
+ * Name: cwd_walk_one
+ *
+ * Description:
+ *   The nxsched_foreach() callback of cwd_walk(): one thread.
+ *
+ ****************************************************************************/
+
+static void cwd_walk_one(FAR struct tcb_s *tcb, FAR void *arg)
+{
+  FAR struct cwd_walk_s *walk = arg;
+  FAR struct task_group_s *group = tcb->group;
+  FAR char *cwd;
+
+  /* The group of an exiting thread can be freed now.  Another group stays:
+   * its release waits for g_cwd_lock in fs_cwd_release().
+   */
+
+  if (group == NULL ||
+      (atomic_read(&tcb->shared_flags) & TCB_FLAG_EXIT_PROCESSING) != 0)
+    {
+      return;
+    }
+
+  cwd = group->tg_cwd;
+  if (cwd == NULL || cwd[0] == '\0' ||
+      !(walk->below ? cwd_under(cwd, walk->path) :
+                      strcmp(cwd, walk->path) == 0) ||
+      (walk->mountpt != NULL && cwd_mounted(walk->mountpt, cwd + walk->len)))
+    {
+      return;
+    }
+
+  if (walk->remove)
+    {
+      cwd[0] = '\0';
+    }
+  else
+    {
+      walk->group = group;
+    }
+}
+
+/****************************************************************************
+ * Name: cwd_walk
+ *
+ * Description:
+ *   Walk the threads of all task groups.  Mark removed each current
+ *   directory at 'path' (or below it), or return a task group with one.
+ *   The caller holds g_cwd_lock.  The walk does not allocate or block.
+ *
+ ****************************************************************************/
+
+static FAR struct task_group_s *cwd_walk(FAR struct cwd_walk_s *walk)
+{
+  walk->group = NULL;
+  nxsched_foreach(cwd_walk_one, walk);
+  return walk->group;
+}
+
+/****************************************************************************
+ * Name: cwd_find
+ *
+ * Description:
+ *   Return true if a current directory is at or below the host path 'path'.
+ *
+ ****************************************************************************/
+
+static bool cwd_find(FAR const char *path)
+{
+  struct cwd_walk_s walk;
+  FAR struct task_group_s *group;
+
+  memset(&walk, 0, sizeof(walk));
+  walk.path  = path;
+  walk.below = true;
+
+  nxmutex_lock(&g_cwd_lock);

Review Comment:
   The critical section of `nxsched_foreach()` stops only the code that also 
enters it. `chdir()` changes and frees `tg_cwd` under `g_cwd_lock` only 
(fs_cwd.c:306-311), so on SMP another CPU can free the path while the walk 
reads it.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to