From 5122e578df1760c5e576682e3f84a1d18dbfca5d Mon Sep 17 00:00:00 2001
From: Xu Chao <xu.chao6@zte.com.cn>
Date: Wed, 24 Nov 2021 10:33:11 +0800
Subject: [PATCH 104/108] util: virExec may blocked by reading pipe if
 grandchild prematurely exit

When VIR_EXEC_DAEMON is set, if virPidFileAcquirePath/virSetInherit failed,
then pipesync[0] can not be closed when granchild process exit, because
pipesync[1] still opened in child process. and then saferead in child
process may blocked forever, and left grandchild process in defunct state.

Signed-off-by: Xu Chao <xu.chao6@zte.com.cn>
Signed-off-by: Yi Wang <wang.yi59@zte.com.cn>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
(cherry picked from commit 6fac961b085af51340246bf51205a9f2f1615962)
---
 src/util/vircommand.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index 8ab83cb8f5..bb824bf60f 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -775,6 +775,10 @@ virExec(virCommandPtr cmd)
         }
 
         if (pid > 0) {
+            /* At this point it's us and the child that holds the write end of
+             * the pipe open. Close the write end of the pipe, so that the pipe
+             * is fully closed if child dies prematurely. */
+            VIR_FORCE_CLOSE(pipesync[1]);
             /* The parent expect us to have written the pid file before
              * exiting. Wait here for the child to write it and signal us. */
             if (cmd->pidfile &&
-- 
2.33.0

