984f77
From d6ffd324cc933efec946a3ffbed6fccfe7077203 Mon Sep 17 00:00:00 2001
984f77
From: Lennart Poettering <lennart@poettering.net>
984f77
Date: Mon, 26 Nov 2018 21:07:48 +0100
984f77
Subject: [PATCH] core: be more careful when inheriting stdout fds to stderr
984f77
984f77
We need to compare the fd name/file name if we inherit an fd from stdout
984f77
to stderr. Let's do that.
984f77
984f77
Fixes: #10875
984f77
(cherry picked from commit 41fc585a7a3b8ae857cad5fdad1bc70cdacfa8e5)
984f77
984f77
Related: #2093479
984f77
---
984f77
 src/core/execute.c | 27 +++++++++++++++++++++++++--
984f77
 1 file changed, 25 insertions(+), 2 deletions(-)
984f77
984f77
diff --git a/src/core/execute.c b/src/core/execute.c
984f77
index 9cbb678ac4..b1d8dceb32 100644
984f77
--- a/src/core/execute.c
984f77
+++ b/src/core/execute.c
984f77
@@ -545,6 +545,30 @@ static int setup_input(
984f77
         }
984f77
 }
984f77
 
984f77
+static bool can_inherit_stderr_from_stdout(
984f77
+                const ExecContext *context,
984f77
+                ExecOutput o,
984f77
+                ExecOutput e) {
984f77
+
984f77
+        assert(context);
984f77
+
984f77
+        /* Returns true, if given the specified STDERR and STDOUT output we can directly dup() the stdout fd to the
984f77
+         * stderr fd */
984f77
+
984f77
+        if (e == EXEC_OUTPUT_INHERIT)
984f77
+                return true;
984f77
+        if (e != o)
984f77
+                return false;
984f77
+
984f77
+        if (e == EXEC_OUTPUT_NAMED_FD)
984f77
+                return streq_ptr(context->stdio_fdname[STDOUT_FILENO], context->stdio_fdname[STDERR_FILENO]);
984f77
+
984f77
+        if (IN_SET(e, EXEC_OUTPUT_FILE, EXEC_OUTPUT_FILE_APPEND))
984f77
+                return streq_ptr(context->stdio_file[STDOUT_FILENO], context->stdio_file[STDERR_FILENO]);
984f77
+
984f77
+        return true;
984f77
+}
984f77
+
984f77
 static int setup_output(
984f77
                 const Unit *unit,
984f77
                 const ExecContext *context,
984f77
@@ -603,7 +627,7 @@ static int setup_output(
984f77
                         return fileno;
984f77
 
984f77
                 /* Duplicate from stdout if possible */
984f77
-                if ((e == o && e != EXEC_OUTPUT_NAMED_FD) || e == EXEC_OUTPUT_INHERIT)
984f77
+                if (can_inherit_stderr_from_stdout(context, o, e))
984f77
                         return dup2(STDOUT_FILENO, fileno) < 0 ? -errno : fileno;
984f77
 
984f77
                 o = e;
984f77
@@ -694,7 +718,6 @@ static int setup_output(
984f77
                         flags |= O_APPEND;
984f77
 
984f77
                 fd = acquire_path(context->stdio_file[fileno], flags, 0666 & ~context->umask);
984f77
-
984f77
                 if (fd < 0)
984f77
                         return fd;
984f77