From 2ce12617a129e337f0bf5ad8552e2a8f1ef525f4 Mon Sep 17 00:00:00 2001 From: Masayuki Ishikawa Date: Wed, 20 Jul 2022 08:47:42 +0900 Subject: [PATCH] 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 --- 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 #include #include +#include +#include #include @@ -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,