696e2f2
From 0da0a63dba426aa351d733292df6ec41603b39b6 Mon Sep 17 00:00:00 2001
696e2f2
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
696e2f2
Date: Thu, 18 Aug 2016 22:03:56 -0400
696e2f2
Subject: [PATCH] systemd: ignore lack of tty when checking whether colors
696e2f2
 should be enabled
696e2f2
696e2f2
When started by the kernel, we are connected to the console, and we'll set TERM
696e2f2
properly to some value in fixup_environment(). We'll then enable or disable
696e2f2
colors based on the value of $SYSTEMD_COLORS and $TERM.
696e2f2
696e2f2
When reexecuting, TERM should be already set, so we can use this value.
696e2f2
Effectively, behaviour is the same as before affd7ed1a was reverted, but instead
696e2f2
of reopening the console before configuring color output, we just ignore what
696e2f2
stdout is connected to and decide based on the variables only.
696e2f2
696e2f2
(cherry picked from commit 158fbf7661912adf0f42c93155499119811dde82)
696e2f2
---
696e2f2
 src/basic/terminal-util.c | 15 +++++++++++----
696e2f2
 1 file changed, 11 insertions(+), 4 deletions(-)
696e2f2
696e2f2
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
696e2f2
index df56d85317..47d31ad4d7 100644
696e2f2
--- a/src/basic/terminal-util.c
696e2f2
+++ b/src/basic/terminal-util.c
696e2f2
@@ -1191,12 +1191,9 @@ int open_terminal_in_namespace(pid_t pid, const char *name, int mode) {
696e2f2
         return receive_one_fd(pair[0], 0);
696e2f2
 }
696e2f2
 
696e2f2
-bool terminal_is_dumb(void) {
696e2f2
+static bool getenv_terminal_is_dumb(void) {
696e2f2
         const char *e;
696e2f2
 
696e2f2
-        if (!on_tty())
696e2f2
-                return true;
696e2f2
-
696e2f2
         e = getenv("TERM");
696e2f2
         if (!e)
696e2f2
                 return true;
696e2f2
@@ -1204,6 +1201,13 @@ bool terminal_is_dumb(void) {
696e2f2
         return streq(e, "dumb");
696e2f2
 }
696e2f2
 
696e2f2
+bool terminal_is_dumb(void) {
696e2f2
+        if (!on_tty())
696e2f2
+                return true;
696e2f2
+
696e2f2
+        return getenv_terminal_is_dumb();
696e2f2
+}
696e2f2
+
696e2f2
 bool colors_enabled(void) {
696e2f2
         static int enabled = -1;
696e2f2
 
696e2f2
@@ -1213,6 +1217,9 @@ bool colors_enabled(void) {
696e2f2
                 colors = getenv("SYSTEMD_COLORS");
696e2f2
                 if (colors)
696e2f2
                         enabled = parse_boolean(colors) != 0;
696e2f2
+                else if (getpid() == 1)
696e2f2
+                        /* PID1 outputs to the console without holding it open all the time */
696e2f2
+                        enabled = !getenv_terminal_is_dumb();
696e2f2
                 else
696e2f2
                         enabled = !terminal_is_dumb();
696e2f2
         }
696e2f2
-- 
696e2f2
2.9.0
696e2f2