[flang-commits] [flang] [clang-tools-extra] [compiler-rt] [llvm] [libc] [clang] [flang] use setsid to assign the child to prevent zombie as it will be clean up by init process (PR #77944)

Yi Wu via flang-commits flang-commits at lists.llvm.org
Thu Jan 18 01:29:03 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/3] 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/3] 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/3] 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 "



More information about the flang-commits mailing list