diff options
author | Mike Yuan <me@yhndnzj.com> | 2023-12-22 11:28:55 +0100 |
---|---|---|
committer | Mike Yuan <me@yhndnzj.com> | 2023-12-22 16:06:49 +0100 |
commit | dd9c8da865932bb0fa63c1210006af0d2d716678 (patch) | |
tree | cae39195ce2a0ede22d2d5baca2774fe4e2314df /src | |
parent | terminal-util: introduce isatty_safe that rejects EBADF (diff) | |
download | systemd-dd9c8da865932bb0fa63c1210006af0d2d716678.tar.xz systemd-dd9c8da865932bb0fa63c1210006af0d2d716678.zip |
various: clean up isatty() handling
As per https://github.com/systemd/systemd/pull/30547#discussion_r1434371627
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/fileio.c | 3 | ||||
-rw-r--r-- | src/basic/log.c | 2 | ||||
-rw-r--r-- | src/basic/terminal-util.c | 10 | ||||
-rw-r--r-- | src/busctl/busctl.c | 2 | ||||
-rw-r--r-- | src/core/exec-invoke.c | 8 | ||||
-rw-r--r-- | src/core/execute.c | 2 | ||||
-rw-r--r-- | src/creds/creds.c | 2 | ||||
-rw-r--r-- | src/getty-generator/getty-generator.c | 5 | ||||
-rw-r--r-- | src/journal/journald.c | 3 | ||||
-rw-r--r-- | src/nspawn/nspawn.c | 9 | ||||
-rw-r--r-- | src/shared/wall.c | 5 | ||||
-rw-r--r-- | src/storagetm/storagetm.c | 4 |
12 files changed, 26 insertions, 29 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c index a050b61db4..752a65646f 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -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; diff --git a/src/basic/log.c b/src/basic/log.c index 1470611a75..34e0ccd956 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -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; } diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index c6ced3f7c1..488541ed23 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -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")) { diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index 9f82198f2f..d233fc55ad 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -1369,7 +1369,7 @@ static int verb_capture(int argc, char **argv, void *userdata) { "busctl (systemd) " STRINGIFY(PROJECT_VERSION) " (Git " GIT_VERSION ")"; int r; - if (isatty(fileno(stdout)) > 0) + if (isatty(STDOUT_FILENO)) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Refusing to write message data to console, please redirect output to a file."); diff --git a/src/core/exec-invoke.c b/src/core/exec-invoke.c index 61f66b6914..0475a3cc3c 100644 --- a/src/core/exec-invoke.c +++ b/src/core/exec-invoke.c @@ -670,12 +670,8 @@ static int chown_terminal(int fd, uid_t uid) { assert(fd >= 0); /* Before we chown/chmod the TTY, let's ensure this is actually a tty */ - if (isatty(fd) < 1) { - if (IN_SET(errno, EINVAL, ENOTTY)) - return 0; /* not a tty */ - - return -errno; - } + if (!isatty_safe(fd)) + return 0; /* This might fail. What matters are the results. */ r = fchmod_and_chown(fd, TTY_MODE, uid, GID_INVALID); diff --git a/src/core/execute.c b/src/core/execute.c index 7b661d70c9..e400d80084 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -147,7 +147,7 @@ void exec_context_tty_reset(const ExecContext *context, const ExecParameters *p) const char *path = exec_context_tty_path(context); - if (p && p->stdin_fd >= 0 && isatty(p->stdin_fd)) + if (p && p->stdin_fd >= 0 && isatty_safe(p->stdin_fd)) fd = p->stdin_fd; else if (path && (context->tty_path || is_terminal_input(context->std_input) || is_terminal_output(context->std_output) || is_terminal_output(context->std_error))) { diff --git a/src/creds/creds.c b/src/creds/creds.c index c9bf2e1e36..b53e8d3b36 100644 --- a/src/creds/creds.c +++ b/src/creds/creds.c @@ -315,7 +315,7 @@ static int print_newline(FILE *f, const char *data, size_t l) { /* Don't bother unless this is a tty */ fd = fileno(f); - if (fd >= 0 && isatty(fd) <= 0) + if (fd >= 0 && !isatty_safe(fd)) return 0; if (fputc('\n', f) != '\n') diff --git a/src/getty-generator/getty-generator.c b/src/getty-generator/getty-generator.c index 7486118365..288f91cedf 100644 --- a/src/getty-generator/getty-generator.c +++ b/src/getty-generator/getty-generator.c @@ -94,9 +94,8 @@ static int verify_tty(const char *name) { if (fd < 0) return -errno; - errno = 0; - if (isatty(fd) <= 0) - return errno_or_else(EIO); + if (!isatty_safe(fd)) + return -errno; return 0; } diff --git a/src/journal/journald.c b/src/journal/journald.c index dab0879d33..2f013c2ab2 100644 --- a/src/journal/journald.c +++ b/src/journal/journald.c @@ -13,6 +13,7 @@ #include "main-func.h" #include "process-util.h" #include "sigbus.h" +#include "terminal-util.h" static int run(int argc, char *argv[]) { _cleanup_(server_freep) Server *s = NULL; @@ -35,7 +36,7 @@ static int run(int argc, char *argv[]) { * daemon when it comes to logging hence LOG_TARGET_AUTO won't do the right thing for * us. Hence explicitly log to the console if we're started from a console or to kmsg * otherwise. */ - log_target = isatty(STDERR_FILENO) > 0 ? LOG_TARGET_CONSOLE : LOG_TARGET_KMSG; + log_target = isatty(STDERR_FILENO) ? LOG_TARGET_CONSOLE : LOG_TARGET_KMSG; log_set_prohibit_ipc(true); /* better safe than sorry */ log_set_target(log_target); diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index b4a74a1e43..fb9df3d053 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -289,7 +289,7 @@ static int handle_arg_console(const char *arg) { else if (streq(arg, "passive")) arg_console_mode = CONSOLE_PASSIVE; else if (streq(arg, "pipe")) { - if (isatty(STDIN_FILENO) > 0 && isatty(STDOUT_FILENO) > 0) + if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) log_full(arg_quiet ? LOG_DEBUG : LOG_NOTICE, "Console mode 'pipe' selected, but standard input/output are connected to an interactive TTY. " "Most likely you want to use 'interactive' console mode for proper interactivity and shell job control. " @@ -297,7 +297,7 @@ static int handle_arg_console(const char *arg) { arg_console_mode = CONSOLE_PIPE; } else if (streq(arg, "autopipe")) { - if (isatty(STDIN_FILENO) > 0 && isatty(STDOUT_FILENO) > 0) + if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) arg_console_mode = CONSOLE_INTERACTIVE; else arg_console_mode = CONSOLE_PIPE; @@ -5766,9 +5766,8 @@ static int run(int argc, char *argv[]) { goto finish; if (arg_console_mode < 0) - arg_console_mode = - isatty(STDIN_FILENO) > 0 && - isatty(STDOUT_FILENO) > 0 ? CONSOLE_INTERACTIVE : CONSOLE_READ_ONLY; + arg_console_mode = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) ? + CONSOLE_INTERACTIVE : CONSOLE_READ_ONLY; if (arg_console_mode == CONSOLE_PIPE) /* if we pass STDERR on to the container, don't add our own logs into it too */ arg_quiet = true; diff --git a/src/shared/wall.c b/src/shared/wall.c index d5900ef019..c5d6439db6 100644 --- a/src/shared/wall.c +++ b/src/shared/wall.c @@ -30,8 +30,9 @@ static int write_to_terminal(const char *tty, const char *message) { fd = open(tty, O_WRONLY|O_NONBLOCK|O_NOCTTY|O_CLOEXEC); if (fd < 0) return -errno; - if (!isatty(fd)) - return -ENOTTY; + + if (!isatty_safe(fd)) + return -errno; return loop_write_full(fd, message, SIZE_MAX, TIMEOUT_USEC); } diff --git a/src/storagetm/storagetm.c b/src/storagetm/storagetm.c index ae63baaf79..a482daabef 100644 --- a/src/storagetm/storagetm.c +++ b/src/storagetm/storagetm.c @@ -1043,7 +1043,7 @@ static int on_display_refresh(sd_event_source *s, uint64_t usec, void *userdata) c->display_refresh_scheduled = false; - if (isatty(STDERR_FILENO) > 0) + if (isatty(STDERR_FILENO)) fputs(ANSI_HOME_CLEAR, stderr); /* If we have both IPv4 and IPv6, we display IPv4 info via Plymouth, since it doesn't have much @@ -1224,7 +1224,7 @@ static int run(int argc, char* argv[]) { if (r < 0) return log_error_errno(r, "Failed to subscribe to RTM_DELADDR events: %m"); - if (isatty(0) > 0) + if (isatty(STDIN_FILENO)) log_info("Hit Ctrl-C to exit target mode."); _unused_ _cleanup_(notify_on_cleanup) const char *notify_message = |