summaryrefslogtreecommitdiffstats
path: root/src/basic/terminal-util.c
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2023-03-16 11:21:28 +0100
committerDaan De Meyer <daan.j.demeyer@gmail.com>2023-03-16 11:21:28 +0100
commit197dd3a9a4bda2f604b437aa395098622678c416 (patch)
tree1d454d306014e3e4c26be0e91862fd63654c0bf5 /src/basic/terminal-util.c
parentdoc: update ELF_PACKAGE_METADATA to use linker flag instead of manual script (diff)
downloadsystemd-197dd3a9a4bda2f604b437aa395098622678c416.tar.xz
systemd-197dd3a9a4bda2f604b437aa395098622678c416.zip
terminal-util: Don't assume terminal is dumb if connected to /dev/null
If we're connected to /dev/null, we're likely going to be writing colors to something that's not stdout/stderr, so let's fall back to checking TERM if that's the case.
Diffstat (limited to '')
-rw-r--r--src/basic/terminal-util.c18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
index 5cf2055dc6..d3aa5c27e2 100644
--- a/src/basic/terminal-util.c
+++ b/src/basic/terminal-util.c
@@ -49,6 +49,7 @@ static volatile unsigned cached_columns = 0;
static volatile unsigned cached_lines = 0;
static volatile int cached_on_tty = -1;
+static volatile int cached_on_dev_null = -1;
static volatile int cached_color_mode = _COLOR_INVALID;
static volatile int cached_underline_enabled = -1;
@@ -894,6 +895,7 @@ void reset_terminal_feature_caches(void) {
cached_color_mode = _COLOR_INVALID;
cached_underline_enabled = -1;
cached_on_tty = -1;
+ cached_on_dev_null = -1;
}
bool on_tty(void) {
@@ -1224,6 +1226,20 @@ int open_terminal_in_namespace(pid_t pid, const char *name, int mode) {
return receive_one_fd(pair[0], 0);
}
+static bool on_dev_null(void) {
+ struct stat dst, ost, est;
+
+ if (cached_on_dev_null >= 0)
+ return cached_on_dev_null;
+
+ if (stat("/dev/null", &dst) < 0 || fstat(STDOUT_FILENO, &ost) < 0 || fstat(STDERR_FILENO, &est) < 0)
+ cached_on_dev_null = false;
+ else
+ cached_on_dev_null = stat_inode_same(&dst, &ost) && stat_inode_same(&dst, &est);
+
+ return cached_on_dev_null;
+}
+
static bool getenv_terminal_is_dumb(void) {
const char *e;
@@ -1235,7 +1251,7 @@ static bool getenv_terminal_is_dumb(void) {
}
bool terminal_is_dumb(void) {
- if (!on_tty())
+ if (!on_tty() && !on_dev_null())
return true;
return getenv_terminal_is_dumb();