[libc-commits] [libc] [clang] [llvm] [flang] [compiler-rt] [clang-tools-extra] [flang] use setsid to assign the child to prevent zombie as it will be clean up by init process (PR #77944)
Yi Wu via libc-commits
libc-commits at lists.llvm.org
Thu Jan 18 07:08:36 PST 2024
https://github.com/yi-wu-arm updated https://github.com/llvm/llvm-project/pull/77944
>From b51f293d57a1ae96fab5d3b2a529186a78643c8c Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 12 Jan 2024 16:44:21 +0000
Subject: [PATCH 1/4] use setsid to assign the child to prevent zombie as it
will be clean up by init process
---
flang/runtime/execute.cpp | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/flang/runtime/execute.cpp b/flang/runtime/execute.cpp
index 48773ae8114b0b..1bd5bb81ec8461 100644
--- a/flang/runtime/execute.cpp
+++ b/flang/runtime/execute.cpp
@@ -180,8 +180,6 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
}
FreeMemory((void *)wcmd);
#else
- // terminated children do not become zombies
- signal(SIGCHLD, SIG_IGN);
pid_t pid{fork()};
if (pid < 0) {
if (!cmdstat) {
@@ -191,6 +189,18 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
CheckAndCopyCharsToDescriptor(cmdmsg, "Fork failed");
}
} else if (pid == 0) {
+ if (setsid() == -1) {
+ if (!cmdstat) {
+ terminator.Crash(
+ "setsid() failed with errno: %d, asynchronous process initiation failed.",
+ errno);
+ } else {
+ StoreIntToDescriptor(cmdstat, ASYNC_NO_SUPPORT_ERR, terminator);
+ CheckAndCopyCharsToDescriptor(
+ cmdmsg, "setsid() failed, asynchronous process initiation failed.");
+ }
+ exit(EXIT_FAILURE);
+ }
int status{std::system(newCmd)};
TerminationCheck(status, cmdstat, cmdmsg, terminator);
exit(status);
>From 9682eb49bcd77e70439accd2eaa4524fea5cdfe5 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 12 Jan 2024 16:58:29 +0000
Subject: [PATCH 2/4] clang-format
---
flang/runtime/execute.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/flang/runtime/execute.cpp b/flang/runtime/execute.cpp
index 1bd5bb81ec8461..d149b5d47ef754 100644
--- a/flang/runtime/execute.cpp
+++ b/flang/runtime/execute.cpp
@@ -191,13 +191,13 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
} else if (pid == 0) {
if (setsid() == -1) {
if (!cmdstat) {
- terminator.Crash(
- "setsid() failed with errno: %d, asynchronous process initiation failed.",
+ terminator.Crash("setsid() failed with errno: %d, asynchronous "
+ "process initiation failed.",
errno);
} else {
StoreIntToDescriptor(cmdstat, ASYNC_NO_SUPPORT_ERR, terminator);
- CheckAndCopyCharsToDescriptor(
- cmdmsg, "setsid() failed, asynchronous process initiation failed.");
+ CheckAndCopyCharsToDescriptor(cmdmsg,
+ "setsid() failed, asynchronous process initiation failed.");
}
exit(EXIT_FAILURE);
}
>From b8f4db41db6ceb10897f113243d4a0954d727dc7 Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 12 Jan 2024 17:01:48 +0000
Subject: [PATCH 3/4] add comment
---
flang/runtime/execute.cpp | 1 +
1 file changed, 1 insertion(+)
diff --git a/flang/runtime/execute.cpp b/flang/runtime/execute.cpp
index d149b5d47ef754..f455cf8b0e88ca 100644
--- a/flang/runtime/execute.cpp
+++ b/flang/runtime/execute.cpp
@@ -189,6 +189,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
CheckAndCopyCharsToDescriptor(cmdmsg, "Fork failed");
}
} else if (pid == 0) {
+ // Create a new session, let init process take care of zombie child
if (setsid() == -1) {
if (!cmdstat) {
terminator.Crash("setsid() failed with errno: %d, asynchronous "
>From bd3279c5a4686f3aa5e7949dc0952c3fad34852e Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Thu, 18 Jan 2024 15:07:18 +0000
Subject: [PATCH 4/4] add tests
---
flang/unittests/Runtime/CommandTest.cpp | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/flang/unittests/Runtime/CommandTest.cpp b/flang/unittests/Runtime/CommandTest.cpp
index 5ae0f1c72f92c3..3fd2667e2930ed 100644
--- a/flang/unittests/Runtime/CommandTest.cpp
+++ b/flang/unittests/Runtime/CommandTest.cpp
@@ -404,6 +404,24 @@ TEST_F(ZeroArguments, ECLInvalidCommandParentNotTerminatedAsync) {
CheckDescriptorEqStr(cmdMsg.get(), "No change");
}
+TEST_F(ZeroArguments, ECLInvalidCommandAsyncDontAffectSync) {
+ OwningPtr<Descriptor> command{CharDescriptor("echo hi")};
+
+ EXPECT_NO_FATAL_FAILURE(RTNAME(ExecuteCommandLine)(
+ *command.get(), false, nullptr, nullptr, nullptr));
+ EXPECT_NO_FATAL_FAILURE(RTNAME(ExecuteCommandLine)(
+ *command.get(), true, nullptr, nullptr, nullptr));
+}
+
+TEST_F(ZeroArguments, ECLInvalidCommandAsyncDontAffectAsync) {
+ OwningPtr<Descriptor> command{CharDescriptor("echo hi")};
+
+ EXPECT_NO_FATAL_FAILURE(RTNAME(ExecuteCommandLine)(
+ *command.get(), false, nullptr, nullptr, nullptr));
+ EXPECT_NO_FATAL_FAILURE(RTNAME(ExecuteCommandLine)(
+ *command.get(), false, nullptr, nullptr, nullptr));
+}
+
static const char *oneArgArgv[]{"aProgram", "anArgumentOfLength20"};
class OneArgument : public CommandFixture {
protected:
More information about the libc-commits
mailing list