terminal-util: introduce isatty_safe that rejects EBADF

This commit is contained in:
Mike Yuan
2023-12-22 23:06:48 +08:00
parent d3f818fea6
commit 76270f5c09
2 changed files with 14 additions and 0 deletions
+12
View File
@@ -54,6 +54,18 @@ static volatile int cached_on_dev_null = -1;
static volatile int cached_color_mode = _COLOR_INVALID;
static volatile int cached_underline_enabled = -1;
bool isatty_safe(int fd) {
assert(fd >= 0);
if (isatty(fd))
return true;
/* Be resilient if we're working on stdio, since they're set up by parent process. */
assert(errno != EBADF || IN_SET(fd, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO));
return false;
}
int chvt(int vt) {
_cleanup_close_ int fd = -EBADF;