You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/07/26 08:04:27 UTC

[incubator-nuttx] 01/02: libs: spawn: Add forkaround for posix_spawn_file_actions_adddup2()

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 2ce12617a129e337f0bf5ad8552e2a8f1ef525f4
Author: Masayuki Ishikawa <ma...@gmail.com>
AuthorDate: Wed Jul 20 08:47:42 2022 +0900

    libs: spawn: Add forkaround for posix_spawn_file_actions_adddup2()
    
    Summary:
    - I noticed that adb shell failed when sh is spawned.
    - Finally, I found that an error happened when executing dup2() action
      if the file descriptor has the O_CLOEXEC option.
    - This commit fixes this issue by dropping the option in the API.
    
    Impact:
    - posix_spawn_file_actions_adddup2() only
    
    Testing:
    - adbd with sabre-6quad:netnsh (will be merged later)
    
    Signed-off-by: Masayuki Ishikawa <Ma...@jp.sony.com>
---
 libs/libc/spawn/lib_psfa_adddup2.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/libs/libc/spawn/lib_psfa_adddup2.c b/libs/libc/spawn/lib_psfa_adddup2.c
index c05e270526..62e14b31bc 100644
--- a/libs/libc/spawn/lib_psfa_adddup2.c
+++ b/libs/libc/spawn/lib_psfa_adddup2.c
@@ -28,6 +28,8 @@
 #include <spawn.h>
 #include <assert.h>
 #include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
 
 #include <nuttx/spawn.h>
 
@@ -64,6 +66,7 @@ int posix_spawn_file_actions_adddup2(
                          int fd1, int fd2)
 {
   FAR struct spawn_dup2_file_action_s *entry;
+  int flags;
 
   DEBUGASSERT(file_actions && fd1 >= 0 && fd2 >= 0);
 
@@ -83,6 +86,12 @@ int posix_spawn_file_actions_adddup2(
   entry->fd1    = fd1;
   entry->fd2    = fd2;
 
+  /* NOTE: Workaround to avoid an error when executing dup2 action */
+
+  flags = fcntl(fd1, F_GETFD);
+  flags &= ~FD_CLOEXEC;
+  fcntl(fd1, F_SETFD, flags);
+
   /* And add it to the file action list */
 
   add_file_action(file_actions,