various: clean up isatty() handling

As per https://github.com/systemd/systemd/pull/30547#discussion_r1434371627
This commit is contained in:
Mike Yuan
2023-12-22 23:06:49 +08:00
parent 76270f5c09
commit dd9c8da865
12 changed files with 26 additions and 29 deletions
+2 -1
View File
@@ -28,6 +28,7 @@
#include "stdio-util.h"
#include "string-util.h"
#include "sync-util.h"
#include "terminal-util.h"
#include "tmpfile-util.h"
/* The maximum size of the file we'll read in one go in read_full_file() (64M). */
@@ -1459,7 +1460,7 @@ int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
* and don't call isatty() on an invalid fd */
flags |= READ_LINE_NOT_A_TTY;
else
flags |= isatty(fd) ? READ_LINE_IS_A_TTY : READ_LINE_NOT_A_TTY;
flags |= isatty_safe(fd) ? READ_LINE_IS_A_TTY : READ_LINE_NOT_A_TTY;
}
if (FLAGS_SET(flags, READ_LINE_IS_A_TTY))
break;
+1 -1
View File
@@ -414,7 +414,7 @@ static bool check_console_fd_is_tty(void) {
return false;
if (console_fd_is_tty < 0)
console_fd_is_tty = isatty(console_fd) > 0;
console_fd_is_tty = isatty_safe(console_fd);
return console_fd_is_tty;
}
+5 -5
View File
@@ -252,7 +252,7 @@ int reset_terminal_fd(int fd, bool switch_to_text) {
assert(fd >= 0);
if (isatty(fd) < 1)
if (!isatty_safe(fd))
return log_debug_errno(errno, "Asked to reset a terminal that actually isn't a terminal: %m");
/* We leave locked terminal attributes untouched, so that Plymouth may set whatever it wants to set,
@@ -359,7 +359,7 @@ int open_terminal(const char *name, int mode) {
c++;
}
if (isatty(fd) < 1)
if (!isatty_safe(fd))
return negative_errno();
return TAKE_FD(fd);
@@ -1468,7 +1468,7 @@ int vt_restore(int fd) {
assert(fd >= 0);
if (isatty(fd) < 1)
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");
if (ioctl(fd, KDSETMODE, KD_TEXT) < 0)
@@ -1495,7 +1495,7 @@ int vt_release(int fd, bool restore) {
* sent by the kernel and optionally reset the VT in text and auto
* VT-switching modes. */
if (isatty(fd) < 1)
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");
if (ioctl(fd, VT_RELDISP, 1) < 0)
@@ -1705,7 +1705,7 @@ int get_default_background_color(double *ret_red, double *ret_green, double *ret
if (!colors_enabled())
return -EOPNOTSUPP;
if (isatty(STDOUT_FILENO) < 1 || isatty(STDIN_FILENO) < 1)
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO))
return -EOPNOTSUPP;
if (streq_ptr(getenv("TERM"), "linux")) {