From 838ff62557e84537731e45462facc99bd2a0df24 Mon Sep 17 00:00:00 2001
From: Simon McVittie <smcv@collabora.com>
Date: Mon, 7 Sep 2020 16:43:56 +0100
Subject: [PATCH] adverb: When a fd is passed through, don't hold it open

This is similar to how we deal with stdout, but for fds that have been
passed through, other than stdin (0), stdout (1) and stderr (2).

Part of pressure-vessel#6.

Signed-off-by: Simon McVittie <smcv@collabora.com>
---
 src/adverb.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/adverb.c b/src/adverb.c
index 46239b366..cec616196 100644
--- a/src/adverb.c
+++ b/src/adverb.c
@@ -868,6 +868,22 @@ main (int argc,
       goto out;
     }
 
+  /* If the parent or child writes to a passed fd and closes it,
+   * don't stand in the way of that. Skip fds 0 (stdin),
+   * 1 (stdout) and 2 (stderr); we have moved our original stdout
+   * to another fd which will be dealt with below, and we want to keep
+   * our stdin and stderr open. */
+  if (child_setup_data.pass_fds != NULL)
+    {
+      gsize i;
+
+      for (i = 0; child_setup_data.pass_fds[i] > -1; i++)
+        {
+          if (child_setup_data.pass_fds[i] > 2)
+            close (child_setup_data.pass_fds[i]);
+        }
+    }
+
   g_free (child_setup_data.pass_fds);
 
   /* If the child writes to stdout and closes it, don't interfere */
-- 
GitLab