diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index 395adc1ea0..40f8282e99 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -61,6 +61,10 @@ static volatile unsigned cached_columns = 0; static volatile unsigned cached_lines = 0; +static volatile int cached_on_tty = -1; +static volatile int cached_colors_enabled = -1; +static volatile int cached_underline_enabled = -1; + int chvt(int vt) { _cleanup_close_ int fd; @@ -626,6 +630,8 @@ int make_console_stdio(void) { if (r < 0) return log_error_errno(r, "Failed to duplicate terminal fd: %m"); + reset_terminal_feature_caches(); + return 0; } @@ -791,7 +797,7 @@ unsigned columns(void) { const char *e; int c; - if (_likely_(cached_columns > 0)) + if (cached_columns > 0) return cached_columns; c = 0; @@ -825,7 +831,7 @@ unsigned lines(void) { const char *e; int l; - if (_likely_(cached_lines > 0)) + if (cached_lines > 0) return cached_lines; l = 0; @@ -849,10 +855,17 @@ void columns_lines_cache_reset(int signum) { cached_lines = 0; } -bool on_tty(void) { - static int cached_on_tty = -1; +void reset_terminal_feature_caches(void) { + cached_columns = 0; + cached_lines = 0; - if (_unlikely_(cached_on_tty < 0)) + cached_colors_enabled = -1; + cached_underline_enabled = -1; + cached_on_tty = -1; +} + +bool on_tty(void) { + if (cached_on_tty < 0) cached_on_tty = isatty(STDOUT_FILENO) > 0; return cached_on_tty; @@ -863,7 +876,7 @@ int make_stdio(int fd) { assert(fd >= 0); - if (dup2(fd, STDIN_FILENO) < 0 && r >= 0) + if (dup2(fd, STDIN_FILENO) < 0) r = -errno; if (dup2(fd, STDOUT_FILENO) < 0 && r >= 0) r = -errno; @@ -880,13 +893,17 @@ int make_stdio(int fd) { } int make_null_stdio(void) { - int null_fd; + int null_fd, r; null_fd = open("/dev/null", O_RDWR|O_NOCTTY|O_CLOEXEC); if (null_fd < 0) return -errno; - return make_stdio(null_fd); + r = make_stdio(null_fd); + + reset_terminal_feature_caches(); + + return r; } int getttyname_malloc(int fd, char **ret) { @@ -1189,38 +1206,36 @@ bool terminal_is_dumb(void) { } bool colors_enabled(void) { - static int enabled = -1; - if (_unlikely_(enabled < 0)) { + if (cached_colors_enabled < 0) { int val; val = getenv_bool("SYSTEMD_COLORS"); if (val >= 0) - enabled = val; + cached_colors_enabled = val; else if (getpid_cached() == 1) /* PID1 outputs to the console without holding it open all the time */ - enabled = !getenv_terminal_is_dumb(); + cached_colors_enabled = !getenv_terminal_is_dumb(); else - enabled = !terminal_is_dumb(); + cached_colors_enabled = !terminal_is_dumb(); } - return enabled; + return cached_colors_enabled; } bool underline_enabled(void) { - static int enabled = -1; - if (enabled < 0) { + if (cached_underline_enabled < 0) { /* The Linux console doesn't support underlining, turn it off, but only there. */ - if (!colors_enabled()) - enabled = false; + if (colors_enabled()) + cached_underline_enabled = !streq_ptr(getenv("TERM"), "linux"); else - enabled = !streq_ptr(getenv("TERM"), "linux"); + cached_underline_enabled = false; } - return enabled; + return cached_underline_enabled; } int vt_default_utf8(void) { diff --git a/src/basic/terminal-util.h b/src/basic/terminal-util.h index 257dbe71da..9a060f5c59 100644 --- a/src/basic/terminal-util.h +++ b/src/basic/terminal-util.h @@ -98,7 +98,9 @@ int fd_columns(int fd); unsigned columns(void); int fd_lines(int fd); unsigned lines(void); + void columns_lines_cache_reset(int _unused_ signum); +void reset_terminal_feature_caches(void); bool on_tty(void); bool terminal_is_dumb(void);