xiaoxiang781216 commented on code in PR #11177:
URL: https://github.com/apache/nuttx/pull/11177#discussion_r1392951414


##########
binfmt/binfmt_copyactions.c:
##########
@@ -0,0 +1,183 @@
+/****************************************************************************
+ * binfmt/binfmt_copyactions.c
+ *
+ * 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 <string.h>
+#include <debug.h>
+#include <errno.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/binfmt/binfmt.h>
+
+#include "binfmt.h"
+
+#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) && 
!defined(CONFIG_BINFMT_DISABLE)
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: binfmt_copyargv
+ *
+ * Description:
+ *   In the kernel build, the file actions will likely lie in the caller's
+ *   address environment and, hence, be inaccessible when we switch to the
+ *   address environment of the new process address environment.  So we
+ *   do not have any real option other than to copy the callers action list.
+ *
+ * Input Parameters:
+ *   copy     - Pointer of file actions
+ *
+ * Returned Value:
+ *   A non-zero copy is returned on success.
+ *
+ ****************************************************************************/
+
+int binfmt_copyactions(FAR posix_spawn_file_actions_t *copy,
+                       FAR const posix_spawn_file_actions_t *actions)
+{
+  FAR struct spawn_general_file_action_s *entry;
+  FAR struct spawn_general_file_action_s *prev;
+  FAR struct spawn_close_file_action_s *close;
+  FAR struct spawn_open_file_action_s *open;
+  FAR struct spawn_dup2_file_action_s *dup2;
+  FAR void *buffer;
+  int size = 0;
+
+  for (entry = (FAR struct spawn_general_file_action_s *)actions;
+       entry != NULL;
+       entry = entry->flink)
+    {
+      switch (entry->action)
+        {
+          case SPAWN_FILE_ACTION_CLOSE:
+            size += sizeof(struct spawn_close_file_action_s);
+            break;
+
+          case SPAWN_FILE_ACTION_DUP2:
+            size += sizeof(struct spawn_dup2_file_action_s);
+            break;
+
+          case SPAWN_FILE_ACTION_OPEN:
+            open = (FAR void *)entry;
+            size += SIZEOF_OPEN_FILE_ACTION_S(strlen(open->path));
+            break;
+
+          default:
+            return -EINVAL;
+        }
+    }
+
+  *copy = buffer = kmm_malloc(size);
+  if (buffer == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  prev = NULL;
+
+  for (entry = (FAR struct spawn_general_file_action_s *)actions,
+       prev = NULL; entry != NULL; prev = entry, entry = entry->flink)
+    {
+      switch (entry->action)
+        {
+          case SPAWN_FILE_ACTION_CLOSE:
+            close = buffer;
+            memcpy(close, entry, sizeof(struct spawn_close_file_action_s));
+            close->flink = NULL;
+            if (prev)
+              {
+                prev->flink = (FAR void *)close;
+              }
+
+            buffer = close + 1;
+            break;
+
+          case SPAWN_FILE_ACTION_DUP2:
+            dup2 = buffer;
+            memcpy(dup2, entry, sizeof(struct spawn_dup2_file_action_s));
+            dup2->flink = NULL;
+            if (prev)
+              {
+                prev->flink = (FAR void *)dup2;
+              }
+
+            buffer = dup2 + 1;
+            break;
+
+          case SPAWN_FILE_ACTION_OPEN:
+            open = buffer;
+            memcpy(open, entry, sizeof(struct spawn_open_file_action_s));
+            open->flink = NULL;
+            if (prev)
+              {
+                prev->flink = (FAR void *)open;
+              }
+
+            strlcpy((FAR void *)(open + 1),

Review Comment:
   ```suggestion
               strlcpy((FAR char *)(open + 1),
   ```



##########
binfmt/Makefile:
##########
@@ -25,7 +25,7 @@ include $(TOPDIR)/Make.defs
 CSRCS  = binfmt_globals.c binfmt_initialize.c binfmt_register.c 
binfmt_unregister.c
 CSRCS += binfmt_loadmodule.c binfmt_unloadmodule.c binfmt_execmodule.c
 CSRCS += binfmt_exec.c binfmt_copyargv.c binfmt_dumpmodule.c
-CSRCS += binfmt_coredump.c
+CSRCS += binfmt_coredump.c binfmt_copyactions.c

Review Comment:
   binfmt_copyactions.c after binfmt_copyargv.c



##########
sched/task/task_spawnparms.c:
##########
@@ -289,3 +291,70 @@ int spawn_file_actions(FAR struct tcb_s *tcb,
 
   return ret;
 }
+
+/****************************************************************************
+ * Name: spawn_file_is_duplicateable
+ *
+ * Description:
+ *   Check the input file descriptor is duplicateable from spawn actions
+ *
+ * Input Parameters:
+ *
+ *   actions - The spawn file actions
+ *   fd      - file descriptor
+ *
+ * Returned Value:
+ *   Return 1 if file descriptor is duplicate able
+ *   Return 0 if file descriptor in action list but unable to duplicate
+ *   Return -1 if file descriptor without in actions list
+ *
+ ****************************************************************************/
+
+int
+spawn_file_is_duplicateable(FAR const posix_spawn_file_actions_t *actions,
+                            int fd)
+{
+  FAR struct spawn_general_file_action_s *entry;
+  FAR struct spawn_close_file_action_s *close;
+  FAR struct spawn_open_file_action_s *open;
+  FAR struct spawn_dup2_file_action_s *dup2;
+
+  /* Execute each file action */
+
+  for (entry = (FAR struct spawn_general_file_action_s *)actions;
+       entry != NULL;
+       entry = entry->flink)
+    {
+      switch (entry->action)
+        {
+          case SPAWN_FILE_ACTION_CLOSE:
+            close = (FAR void *)entry;

Review Comment:
   why not cast to spawn_close_file_action_s



##########
sched/task/task_spawnparms.c:
##########
@@ -289,3 +291,70 @@ int spawn_file_actions(FAR struct tcb_s *tcb,
 
   return ret;
 }
+
+/****************************************************************************
+ * Name: spawn_file_is_duplicateable
+ *
+ * Description:
+ *   Check the input file descriptor is duplicateable from spawn actions
+ *
+ * Input Parameters:
+ *
+ *   actions - The spawn file actions
+ *   fd      - file descriptor
+ *
+ * Returned Value:
+ *   Return 1 if file descriptor is duplicate able
+ *   Return 0 if file descriptor in action list but unable to duplicate
+ *   Return -1 if file descriptor without in actions list
+ *
+ ****************************************************************************/
+
+int
+spawn_file_is_duplicateable(FAR const posix_spawn_file_actions_t *actions,
+                            int fd)
+{
+  FAR struct spawn_general_file_action_s *entry;
+  FAR struct spawn_close_file_action_s *close;
+  FAR struct spawn_open_file_action_s *open;
+  FAR struct spawn_dup2_file_action_s *dup2;
+
+  /* Execute each file action */
+
+  for (entry = (FAR struct spawn_general_file_action_s *)actions;
+       entry != NULL;
+       entry = entry->flink)
+    {
+      switch (entry->action)
+        {
+          case SPAWN_FILE_ACTION_CLOSE:
+            close = (FAR void *)entry;
+            if (close->fd == fd)
+              {
+                return 0;
+              }
+            break;
+
+          case SPAWN_FILE_ACTION_DUP2:
+            dup2 = (FAR void *)entry;

Review Comment:
   ditto



##########
binfmt/binfmt_execmodule.c:
##########
@@ -310,12 +317,14 @@ int exec_module(FAR struct binary_s *binp,
   if (argv && argv[0])
     {
       ret = nxtask_init(tcb, argv[0], binp->priority, stackaddr,
-                        binp->stacksize, binp->entrypt, &argv[1], envp);
+                        binp->stacksize, binp->entrypt, &argv[1],
+                        envp, actions);

Review Comment:
   ```suggestion
                           envp, factions);
   ```



##########
binfmt/binfmt_copyactions.c:
##########
@@ -0,0 +1,183 @@
+/****************************************************************************
+ * binfmt/binfmt_copyactions.c
+ *
+ * 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 <string.h>
+#include <debug.h>
+#include <errno.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/binfmt/binfmt.h>
+
+#include "binfmt.h"
+
+#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) && 
!defined(CONFIG_BINFMT_DISABLE)
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: binfmt_copyargv
+ *
+ * Description:
+ *   In the kernel build, the file actions will likely lie in the caller's
+ *   address environment and, hence, be inaccessible when we switch to the
+ *   address environment of the new process address environment.  So we
+ *   do not have any real option other than to copy the callers action list.
+ *
+ * Input Parameters:
+ *   copy     - Pointer of file actions
+ *
+ * Returned Value:
+ *   A non-zero copy is returned on success.
+ *
+ ****************************************************************************/
+
+int binfmt_copyactions(FAR posix_spawn_file_actions_t *copy,
+                       FAR const posix_spawn_file_actions_t *actions)
+{
+  FAR struct spawn_general_file_action_s *entry;
+  FAR struct spawn_general_file_action_s *prev;
+  FAR struct spawn_close_file_action_s *close;
+  FAR struct spawn_open_file_action_s *open;
+  FAR struct spawn_dup2_file_action_s *dup2;
+  FAR void *buffer;
+  int size = 0;
+
+  for (entry = (FAR struct spawn_general_file_action_s *)actions;
+       entry != NULL;
+       entry = entry->flink)
+    {
+      switch (entry->action)
+        {
+          case SPAWN_FILE_ACTION_CLOSE:
+            size += sizeof(struct spawn_close_file_action_s);
+            break;
+
+          case SPAWN_FILE_ACTION_DUP2:
+            size += sizeof(struct spawn_dup2_file_action_s);
+            break;
+
+          case SPAWN_FILE_ACTION_OPEN:
+            open = (FAR void *)entry;

Review Comment:
   ```suggestion
               open = (FAR struct spawn_open_file_action_s *)entry;
   ```



##########
sched/task/task_spawnparms.c:
##########
@@ -289,3 +291,70 @@ int spawn_file_actions(FAR struct tcb_s *tcb,
 
   return ret;
 }
+
+/****************************************************************************
+ * Name: spawn_file_is_duplicateable
+ *
+ * Description:
+ *   Check the input file descriptor is duplicateable from spawn actions
+ *
+ * Input Parameters:
+ *
+ *   actions - The spawn file actions
+ *   fd      - file descriptor
+ *
+ * Returned Value:
+ *   Return 1 if file descriptor is duplicate able
+ *   Return 0 if file descriptor in action list but unable to duplicate
+ *   Return -1 if file descriptor without in actions list
+ *
+ ****************************************************************************/
+
+int

Review Comment:
   int to bool



##########
binfmt/binfmt_copyactions.c:
##########
@@ -0,0 +1,183 @@
+/****************************************************************************
+ * binfmt/binfmt_copyactions.c
+ *
+ * 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 <string.h>
+#include <debug.h>
+#include <errno.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/binfmt/binfmt.h>
+
+#include "binfmt.h"
+
+#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) && 
!defined(CONFIG_BINFMT_DISABLE)
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: binfmt_copyargv
+ *
+ * Description:
+ *   In the kernel build, the file actions will likely lie in the caller's
+ *   address environment and, hence, be inaccessible when we switch to the
+ *   address environment of the new process address environment.  So we
+ *   do not have any real option other than to copy the callers action list.
+ *
+ * Input Parameters:
+ *   copy     - Pointer of file actions
+ *
+ * Returned Value:
+ *   A non-zero copy is returned on success.
+ *
+ ****************************************************************************/
+
+int binfmt_copyactions(FAR posix_spawn_file_actions_t *copy,
+                       FAR const posix_spawn_file_actions_t *actions)
+{
+  FAR struct spawn_general_file_action_s *entry;
+  FAR struct spawn_general_file_action_s *prev;
+  FAR struct spawn_close_file_action_s *close;
+  FAR struct spawn_open_file_action_s *open;
+  FAR struct spawn_dup2_file_action_s *dup2;
+  FAR void *buffer;
+  int size = 0;
+
+  for (entry = (FAR struct spawn_general_file_action_s *)actions;
+       entry != NULL;
+       entry = entry->flink)
+    {
+      switch (entry->action)
+        {
+          case SPAWN_FILE_ACTION_CLOSE:
+            size += sizeof(struct spawn_close_file_action_s);
+            break;
+
+          case SPAWN_FILE_ACTION_DUP2:
+            size += sizeof(struct spawn_dup2_file_action_s);
+            break;
+
+          case SPAWN_FILE_ACTION_OPEN:
+            open = (FAR void *)entry;
+            size += SIZEOF_OPEN_FILE_ACTION_S(strlen(open->path));
+            break;
+
+          default:
+            return -EINVAL;
+        }
+    }
+
+  *copy = buffer = kmm_malloc(size);
+  if (buffer == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  prev = NULL;
+
+  for (entry = (FAR struct spawn_general_file_action_s *)actions,
+       prev = NULL; entry != NULL; prev = entry, entry = entry->flink)
+    {
+      switch (entry->action)
+        {
+          case SPAWN_FILE_ACTION_CLOSE:
+            close = buffer;
+            memcpy(close, entry, sizeof(struct spawn_close_file_action_s));
+            close->flink = NULL;
+            if (prev)
+              {
+                prev->flink = (FAR void *)close;
+              }
+
+            buffer = close + 1;
+            break;
+
+          case SPAWN_FILE_ACTION_DUP2:
+            dup2 = buffer;
+            memcpy(dup2, entry, sizeof(struct spawn_dup2_file_action_s));
+            dup2->flink = NULL;
+            if (prev)
+              {
+                prev->flink = (FAR void *)dup2;
+              }
+
+            buffer = dup2 + 1;
+            break;
+
+          case SPAWN_FILE_ACTION_OPEN:
+            open = buffer;
+            memcpy(open, entry, sizeof(struct spawn_open_file_action_s));
+            open->flink = NULL;
+            if (prev)
+              {
+                prev->flink = (FAR void *)open;
+              }
+
+            strlcpy((FAR void *)(open + 1),
+                    ((struct spawn_open_file_action_s *)entry)->path,

Review Comment:
   should we use a temp variable to avoid casting twice?



##########
fs/inode/fs_files.c:
##########
@@ -408,7 +409,9 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR 
struct inode *inode,
       return ret;
     }
 
-  filep = files_fget_by_index(list, i, 0);
+  j = 0;
+
+  filep = files_fget_by_index(list, i, j);

Review Comment:
   ```suggestion
     filep = files_fget_by_index(list, i, j = 0);
   ```
   move to separated patch?



##########
binfmt/binfmt_execmodule.c:
##########
@@ -256,6 +257,12 @@ int exec_module(FAR struct binary_s *binp,
       goto errout_with_args;
     }
 
+  ret = binfmt_copyactions(&factions, actions);

Review Comment:
   why not use the same variable like binfmt_copyargv



##########
binfmt/binfmt_copyactions.c:
##########
@@ -0,0 +1,183 @@
+/****************************************************************************
+ * binfmt/binfmt_copyactions.c
+ *
+ * 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 <string.h>
+#include <debug.h>
+#include <errno.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/binfmt/binfmt.h>
+
+#include "binfmt.h"
+
+#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) && 
!defined(CONFIG_BINFMT_DISABLE)
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: binfmt_copyargv

Review Comment:
   ```suggestion
    * Name: binfmt_copyactions
   ```



##########
sched/task/task_spawnparms.c:
##########
@@ -289,3 +291,70 @@ int spawn_file_actions(FAR struct tcb_s *tcb,
 
   return ret;
 }
+
+/****************************************************************************
+ * Name: spawn_file_is_duplicateable
+ *
+ * Description:
+ *   Check the input file descriptor is duplicateable from spawn actions
+ *
+ * Input Parameters:
+ *
+ *   actions - The spawn file actions
+ *   fd      - file descriptor
+ *
+ * Returned Value:
+ *   Return 1 if file descriptor is duplicate able
+ *   Return 0 if file descriptor in action list but unable to duplicate
+ *   Return -1 if file descriptor without in actions list
+ *
+ ****************************************************************************/
+
+int
+spawn_file_is_duplicateable(FAR const posix_spawn_file_actions_t *actions,
+                            int fd)
+{
+  FAR struct spawn_general_file_action_s *entry;
+  FAR struct spawn_close_file_action_s *close;
+  FAR struct spawn_open_file_action_s *open;
+  FAR struct spawn_dup2_file_action_s *dup2;
+
+  /* Execute each file action */
+
+  for (entry = (FAR struct spawn_general_file_action_s *)actions;
+       entry != NULL;
+       entry = entry->flink)
+    {
+      switch (entry->action)
+        {
+          case SPAWN_FILE_ACTION_CLOSE:
+            close = (FAR void *)entry;
+            if (close->fd == fd)
+              {
+                return 0;
+              }
+            break;
+
+          case SPAWN_FILE_ACTION_DUP2:
+            dup2 = (FAR void *)entry;
+            if (dup2->fd1 == fd)
+              {
+                return 1;
+              }
+            break;
+
+          case SPAWN_FILE_ACTION_OPEN:
+            open = (FAR void *)entry;

Review Comment:
   ditto



##########
include/nuttx/spawn.h:
##########
@@ -100,9 +100,14 @@ extern "C"
 void add_file_action(FAR posix_spawn_file_actions_t *file_action,
                      FAR struct spawn_general_file_action_s *entry);
 
+struct tcb_s;

Review Comment:
   why need



-- 
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: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to