Просмотр исходного кода

!314 修改每个进程有独立工作目录
Merge pull request !314 from lin123/rt-smart

bernard 4 лет назад
Родитель
Сommit
5f0122a13d
4 измененных файлов с 105 добавлено и 18 удалено
  1. 16 13
      components/dfs/src/dfs.c
  2. 37 3
      components/dfs/src/dfs_posix.c
  3. 47 0
      components/lwp/lwp.c
  4. 5 2
      components/lwp/lwp.h

+ 16 - 13
components/dfs/src/dfs.c

@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
  *
  *
  * SPDX-License-Identifier: Apache-2.0
  * SPDX-License-Identifier: Apache-2.0
  *
  *
@@ -14,6 +14,7 @@
 #include <dfs_fs.h>
 #include <dfs_fs.h>
 #include <dfs_file.h>
 #include <dfs_file.h>
 #include "dfs_private.h"
 #include "dfs_private.h"
+
 #ifdef RT_USING_LWP
 #ifdef RT_USING_LWP
 #include <lwp.h>
 #include <lwp.h>
 #endif
 #endif
@@ -447,7 +448,7 @@ static int fd_get_fd_index_form_fdt(struct dfs_fdtable *fdt, struct dfs_fd *file
 {
 {
     int fd = -1;
     int fd = -1;
 
 
-    if (file == RT_NULL) 
+    if (file == RT_NULL)
     {
     {
         return -1;
         return -1;
     }
     }
@@ -458,8 +459,8 @@ static int fd_get_fd_index_form_fdt(struct dfs_fdtable *fdt, struct dfs_fd *file
     {
     {
         if(fdt->fds[index] == file)
         if(fdt->fds[index] == file)
         {
         {
-            fd = index; 
-            break; 
+            fd = index;
+            break;
         }
         }
     }
     }
 
 
@@ -565,7 +566,9 @@ char *dfs_normalize_path(const char *directory, const char *filename)
 
 
 #ifdef DFS_USING_WORKDIR
 #ifdef DFS_USING_WORKDIR
     if (directory == NULL) /* shall use working directory */
     if (directory == NULL) /* shall use working directory */
-        directory = &working_directory[0];
+    {
+        directory = lwp_getcwd();
+    }
 #else
 #else
     if ((directory == NULL) && (filename[0] != '/'))
     if ((directory == NULL) && (filename[0] != '/'))
     {
     {
@@ -707,7 +710,7 @@ struct dfs_fdtable *dfs_fdtable_get_pid(int pid)
     struct rt_lwp *lwp = RT_NULL;
     struct rt_lwp *lwp = RT_NULL;
     struct dfs_fdtable *fdt = RT_NULL;
     struct dfs_fdtable *fdt = RT_NULL;
 
 
-    lwp = lwp_from_pid(pid); 
+    lwp = lwp_from_pid(pid);
     if (lwp)
     if (lwp)
     {
     {
         fdt = &lwp->fdt;
         fdt = &lwp->fdt;
@@ -715,7 +718,7 @@ struct dfs_fdtable *dfs_fdtable_get_pid(int pid)
 
 
     return fdt;
     return fdt;
 }
 }
-#endif 
+#endif
 
 
 struct dfs_fdtable *dfs_fdtable_get_global(void)
 struct dfs_fdtable *dfs_fdtable_get_global(void)
 {
 {
@@ -781,7 +784,7 @@ static int lsofp(int pid)
     else
     else
     {
     {
         fd_table = dfs_fdtable_get_pid(pid);
         fd_table = dfs_fdtable_get_pid(pid);
-        if (!fd_table) 
+        if (!fd_table)
         {
         {
             rt_kprintf("PID %s is not a applet(lwp)\n", pid);
             rt_kprintf("PID %s is not a applet(lwp)\n", pid);
             return -1;
             return -1;
@@ -849,7 +852,7 @@ int lsof(int argc, char *argv[])
         struct rt_list_node *node, *list;
         struct rt_list_node *node, *list;
         struct lwp_avl_struct *pids = lwp_get_pid_ary();
         struct lwp_avl_struct *pids = lwp_get_pid_ary();
 
 
-        lsofp(-1); 
+        lsofp(-1);
 
 
         for (int index = 0; index < RT_LWP_MAX_NR; index++)
         for (int index = 0; index < RT_LWP_MAX_NR; index++)
         {
         {
@@ -860,7 +863,7 @@ int lsof(int argc, char *argv[])
                 list = &lwp->t_grp;
                 list = &lwp->t_grp;
                 for (node = list->next; node != list; node = node->next)
                 for (node = list->next; node != list; node = node->next)
                 {
                 {
-                    lsofp(lwp_to_pid(lwp)); 
+                    lsofp(lwp_to_pid(lwp));
                 }
                 }
             }
             }
         }
         }
@@ -870,14 +873,14 @@ int lsof(int argc, char *argv[])
         if (argv[1][0] == '-' && argv[1][1] == 'p')
         if (argv[1][0] == '-' && argv[1][1] == 'p')
         {
         {
             int pid = atoi(argv[2]);
             int pid = atoi(argv[2]);
-            lsofp(pid); 
+            lsofp(pid);
         }
         }
     }
     }
 
 
     return 0;
     return 0;
 }
 }
-MSH_CMD_EXPORT(lsof, list open files); 
-#endif /* RT_USING_LWP */ 
+MSH_CMD_EXPORT(lsof, list open files);
+#endif /* RT_USING_LWP */
 
 
 /*
 /*
  * If no argument is specified, display the mount history;
  * If no argument is specified, display the mount history;

+ 37 - 3
components/dfs/src/dfs_posix.c

@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 2006-2018, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
  *
  *
  * SPDX-License-Identifier: Apache-2.0
  * SPDX-License-Identifier: Apache-2.0
  *
  *
@@ -7,12 +7,17 @@
  * Date           Author       Notes
  * Date           Author       Notes
  * 2009-05-27     Yi.qiu       The first version
  * 2009-05-27     Yi.qiu       The first version
  * 2018-02-07     Bernard      Change the 3rd parameter of open/fcntl/ioctl to '...'
  * 2018-02-07     Bernard      Change the 3rd parameter of open/fcntl/ioctl to '...'
+ * 2021-08-26     linzhenxing  add setcwd and modify getcwd\chdir
  */
  */
 
 
 #include <dfs.h>
 #include <dfs.h>
 #include <dfs_posix.h>
 #include <dfs_posix.h>
 #include "dfs_private.h"
 #include "dfs_private.h"
 
 
+#ifdef RT_USING_LWP
+#include <lwp.h>
+#endif
+
 /**
 /**
  * @addtogroup FsPosixApi
  * @addtogroup FsPosixApi
  */
  */
@@ -815,7 +820,9 @@ int chdir(const char *path)
     if (path == NULL)
     if (path == NULL)
     {
     {
         dfs_lock();
         dfs_lock();
+#ifdef DFS_USING_WORKDIR
         rt_kprintf("%s\n", working_directory);
         rt_kprintf("%s\n", working_directory);
+#endif
         dfs_unlock();
         dfs_unlock();
 
 
         return 0;
         return 0;
@@ -851,7 +858,7 @@ int chdir(const char *path)
     closedir(d);
     closedir(d);
 
 
     /* copy full path to working directory */
     /* copy full path to working directory */
-    strncpy(working_directory, fullpath, DFS_PATH_MAX);
+    lwp_setcwd(fullpath);
     /* release normalize directory path name */
     /* release normalize directory path name */
     rt_free(fullpath);
     rt_free(fullpath);
 
 
@@ -884,6 +891,27 @@ int access(const char *path, int amode)
     /* ignore R_OK,W_OK,X_OK condition */
     /* ignore R_OK,W_OK,X_OK condition */
     return 0;
     return 0;
 }
 }
+/**
+ * this function is a POSIX compliant version, which will set current
+ * working directory.
+ *
+ * @param buf the current directory.
+ *
+ * @return null.
+ */
+void setcwd(char *buf)
+{
+#ifdef DFS_USING_WORKDIR
+    dfs_lock();
+    lwp_setcwd(buf);
+    dfs_unlock();
+#else
+    rt_kprintf(NO_WORKING_DIR);
+#endif
+
+    return ;
+}
+RTM_EXPORT(setcwd);
 
 
 /**
 /**
  * this function is a POSIX compliant version, which will return current
  * this function is a POSIX compliant version, which will return current
@@ -896,9 +924,15 @@ int access(const char *path, int amode)
  */
  */
 char *getcwd(char *buf, size_t size)
 char *getcwd(char *buf, size_t size)
 {
 {
+    char *dir_buf = RT_NULL;
 #ifdef DFS_USING_WORKDIR
 #ifdef DFS_USING_WORKDIR
     dfs_lock();
     dfs_lock();
-    strncpy(buf, working_directory, size);
+    dir_buf = lwp_getcwd();
+    if(dir_buf[0] != '/')
+    {
+        dir_buf = &working_directory[0];
+    }
+    rt_strncpy(buf, dir_buf, size);
     dfs_unlock();
     dfs_unlock();
 #else
 #else
     rt_kprintf(NO_WORKING_DIR);
     rt_kprintf(NO_WORKING_DIR);

+ 47 - 0
components/lwp/lwp.c

@@ -8,6 +8,7 @@
  * 2006-03-12     Bernard      first version
  * 2006-03-12     Bernard      first version
  * 2018-11-02     heyuanjie    fix complie error in iar
  * 2018-11-02     heyuanjie    fix complie error in iar
  * 2021-02-03     lizhirui     add 64-bit arch support and riscv64 arch support
  * 2021-02-03     lizhirui     add 64-bit arch support and riscv64 arch support
+ * 2021-08-26     linzhenxing  add lwp_setcwd\lwp_getcwd
  */
  */
 
 
 #include <rthw.h>
 #include <rthw.h>
@@ -39,11 +40,57 @@
 #endif
 #endif
 
 
 static const char elf_magic[] = {0x7f, 'E', 'L', 'F'};
 static const char elf_magic[] = {0x7f, 'E', 'L', 'F'};
+#ifdef DFS_USING_WORKDIR
+extern char working_directory[];
+#endif
 
 
 extern void lwp_user_entry(void *args, const void *text, void *ustack, void *k_stack);
 extern void lwp_user_entry(void *args, const void *text, void *ustack, void *k_stack);
 extern int libc_stdio_get_console(void);
 extern int libc_stdio_get_console(void);
 int load_ldso(struct rt_lwp *lwp, char *exec_name, char *const argv[], char *const envp[]);
 int load_ldso(struct rt_lwp *lwp, char *exec_name, char *const argv[], char *const envp[]);
 
 
+void lwp_setcwd(char *buf)
+{
+    if(strlen(buf) >= DFS_PATH_MAX)
+    {
+        rt_kprintf("buf too long!\n");
+        return ;
+    }
+
+#ifdef RT_USING_LWP
+    struct rt_lwp *lwp;
+
+    lwp = (struct rt_lwp *)rt_thread_self()->lwp;
+    if (lwp)
+        rt_strncpy(lwp->working_directory, buf, DFS_PATH_MAX);
+    else
+        rt_strncpy(working_directory, buf, DFS_PATH_MAX);
+#else
+#ifdef DFS_USING_WORKDIR
+    rt_strncpy(working_directory, buf, DFS_PATH_MAX);
+#endif
+#endif
+    return ;
+}
+
+char *lwp_getcwd(void)
+{
+    char *dir_buf = RT_NULL;
+#ifdef RT_USING_LWP
+    struct rt_lwp *lwp;
+
+    lwp = (struct rt_lwp *)rt_thread_self()->lwp;
+    if (lwp)
+        dir_buf = &lwp->working_directory[0];
+    else
+        dir_buf = &working_directory[0];
+#else
+#ifdef DFS_USING_WORKDIR
+    dir_buf = &working_directory[0];
+#endif
+#endif
+    return dir_buf;
+}
+
 /**
 /**
  * RT-Thread light-weight process
  * RT-Thread light-weight process
  */
  */

+ 5 - 2
components/lwp/lwp.h

@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
  *
  *
  * SPDX-License-Identifier: Apache-2.0
  * SPDX-License-Identifier: Apache-2.0
  *
  *
@@ -8,6 +8,7 @@
  * 2018-06-29     heyuanjie    first version
  * 2018-06-29     heyuanjie    first version
  * 2019-10-12     Jesven       Add MMU and userspace support
  * 2019-10-12     Jesven       Add MMU and userspace support
  * 2020-10-08     Bernard      Architecture and code cleanup
  * 2020-10-08     Bernard      Architecture and code cleanup
+ * 2021-08-26     linzhenxing  add lwp_setcwd\lwp_getcwd
  */
  */
 
 
 /*
 /*
@@ -96,6 +97,7 @@ struct rt_lwp
     struct rt_wqueue wait_queue; /*for console */
     struct rt_wqueue wait_queue; /*for console */
 
 
     struct lwp_avl_struct *address_search_head; /* for addressed object fast rearch */
     struct lwp_avl_struct *address_search_head; /* for addressed object fast rearch */
+    char working_directory[DFS_PATH_MAX];
 #ifdef RT_USING_GDBSERVER
 #ifdef RT_USING_GDBSERVER
     int debug;
     int debug;
     uint32_t bak_first_ins;
     uint32_t bak_first_ins;
@@ -110,7 +112,8 @@ enum lwp_exit_request_type
     LWP_EXIT_REQUEST_TRIGGERED,
     LWP_EXIT_REQUEST_TRIGGERED,
     LWP_EXIT_REQUEST_IN_PROCESS,
     LWP_EXIT_REQUEST_IN_PROCESS,
 };
 };
-
+void lwp_setcwd(char *buf);
+char *lwp_getcwd(void);
 void lwp_request_thread_exit(rt_thread_t thread_to_exit);
 void lwp_request_thread_exit(rt_thread_t thread_to_exit);
 int  lwp_check_exit_request(void);
 int  lwp_check_exit_request(void);
 void lwp_terminate(struct rt_lwp *lwp);
 void lwp_terminate(struct rt_lwp *lwp);