From: Mike Yuan Date: Fri, 16 May 2025 19:50:29 +0000 (+0200) Subject: terminal-util: restore terminal settings if fd_reopen() fails X-Git-Tag: v258-rc1~577 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=13cb6641815d8fcbad26502f6972c9ecb34b3248;p=thirdparty%2Fsystemd.git terminal-util: restore terminal settings if fd_reopen() fails Follow-up for a0c314d6b04c0a20d566b15c759d18cdb2916c14 --- diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c index b2daf689358..00cdd02eca8 100644 --- a/src/basic/terminal-util.c +++ b/src/basic/terminal-util.c @@ -2131,9 +2131,9 @@ int get_default_background_color(double *ret_red, double *ret_green, double *ret /* Open a 2nd input fd, in non-blocking mode, so that we won't ever hang in read() should someone * else process the POLLIN. */ - nonblock_input_fd = fd_reopen(STDIN_FILENO, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); - if (nonblock_input_fd < 0) - return nonblock_input_fd; + nonblock_input_fd = r = fd_reopen(STDIN_FILENO, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); + if (r < 0) + goto finish; usec_t end = usec_add(now(CLOCK_MONOTONIC), 333 * USEC_PER_MSEC); char buf[STRLEN(ANSI_OSC "11;rgb:0/0/0" ANSI_ST)]; /* shortest possible reply */ @@ -2284,12 +2284,11 @@ int terminal_get_size_by_dsr( unsigned *ret_columns) { _cleanup_close_ int nonblock_input_fd = -EBADF; + int r; assert(input_fd >= 0); assert(output_fd >= 0); - int r; - /* Tries to determine the terminal dimension by means of ANSI sequences rather than TIOCGWINSZ * ioctl(). Why bother with this? The ioctl() information is often incorrect on serial terminals * (since there's no handshake or protocol to determine the right dimensions in RS232), but since the @@ -2335,9 +2334,9 @@ int terminal_get_size_by_dsr( /* Open a 2nd input fd, in non-blocking mode, so that we won't ever hang in read() should someone * else process the POLLIN. */ - nonblock_input_fd = fd_reopen(input_fd, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); - if (nonblock_input_fd < 0) - return nonblock_input_fd; + nonblock_input_fd = r = fd_reopen(input_fd, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); + if (r < 0) + goto finish; usec_t end = usec_add(now(CLOCK_MONOTONIC), 333 * USEC_PER_MSEC); char buf[STRLEN("\x1B[1;1R")]; /* The shortest valid reply possible */