mirror of
https://github.com/systemd/systemd.git
synced 2025-01-26 14:04:03 +03:00
terminal-util: when making /dev/null or the console stdio, forget cached terminal features
Let's forget all relevant terminal features we learnt when we make a console or /dev/null stdin/stdout/stderr. Also, while we are at it, let's drop the various _unlikely_ and _likely_ annotiations around the terminal feature caches. In many cases we call the relevant functions only once in which cases the annotations are likely to do just harm and no good. After all we can't know if the specific code will call us just once or many times...
This commit is contained in:
parent
8854d79504
commit
c6063244db
@ -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) {
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user