Merge pull request #34044 from poettering/isatty-fixes

fixes around isatty() handling
This commit is contained in:
Yu Watanabe
2024-08-20 20:36:07 +09:00
committed by GitHub
21 changed files with 47 additions and 37 deletions
+11 -5
View File
@@ -65,6 +65,12 @@ bool isatty_safe(int fd) {
if (isatty(fd))
return true;
/* Linux/glibc returns EIO for hung up TTY on isatty(). Which is wrong, the thing doesn't stop being
* a TTY after all, just because it is temporarily hung up. Let's work around this here, until this
* is fixed in glibc. See: https://sourceware.org/bugzilla/show_bug.cgi?id=32103 */
if (errno == EIO)
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));
@@ -281,7 +287,7 @@ int open_terminal(const char *name, int mode) {
}
if (!isatty_safe(fd))
return negative_errno();
return -ENOTTY;
return TAKE_FD(fd);
}
@@ -1046,8 +1052,8 @@ bool on_tty(void) {
if (cached_on_tty < 0)
cached_on_tty =
isatty(STDOUT_FILENO) > 0 &&
isatty(STDERR_FILENO) > 0;
isatty_safe(STDOUT_FILENO) &&
isatty_safe(STDERR_FILENO);
return cached_on_tty;
}
@@ -1502,7 +1508,7 @@ int vt_restore(int fd) {
assert(fd >= 0);
if (!isatty_safe(fd))
return log_debug_errno(errno, "Asked to restore the VT for an fd that does not refer to a terminal: %m");
return log_debug_errno(SYNTHETIC_ERRNO(ENOTTY), "Asked to restore the VT for an fd that does not refer to a terminal: %m");
if (ioctl(fd, KDSETMODE, KD_TEXT) < 0)
RET_GATHER(ret, log_debug_errno(errno, "Failed to set VT to text mode, ignoring: %m"));
@@ -1529,7 +1535,7 @@ int vt_release(int fd, bool restore) {
* VT-switching modes. */
if (!isatty_safe(fd))
return log_debug_errno(errno, "Asked to release the VT for an fd that does not refer to a terminal: %m");
return log_debug_errno(SYNTHETIC_ERRNO(ENOTTY), "Asked to release the VT for an fd that does not refer to a terminal: %m");
if (ioctl(fd, VT_RELDISP, 1) < 0)
return -errno;
+2 -1
View File
@@ -27,6 +27,7 @@
#include "random-util.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "user-util.h"
#include "utf8.h"
@@ -120,7 +121,7 @@ char* getlogname_malloc(void) {
uid_t uid;
struct stat st;
if (isatty(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
if (isatty_safe(STDIN_FILENO) && fstat(STDIN_FILENO, &st) >= 0)
uid = st.st_uid;
else
uid = getuid();