]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
terminal-util: restore terminal settings if fd_reopen() fails
authorMike Yuan <me@yhndnzj.com>
Fri, 16 May 2025 19:50:29 +0000 (21:50 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Sat, 17 May 2025 11:33:26 +0000 (12:33 +0100)
Follow-up for a0c314d6b04c0a20d566b15c759d18cdb2916c14

src/basic/terminal-util.c

index b2daf689358bae4f741eaa38187851bd3fa80d2c..00cdd02eca84c867e54d809d6647342015bdab78 100644 (file)
@@ -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 */